Sponsored Ad

Saturday, March 12, 2011

How to Fix C# Error: Cannot implicitly convert type 'string' to 'bool'

While programming suddenly i got the error : Cannot implicitly convert type 'string' to 'bool'. Then i relies that C# usage == (double equal to ) as a compare operator while = (single equal to ) is a assignment operator. So take care of this type of mistakes.

Program with error:

using System;

namespace MyApp
{
    class my_class
    {
        static void Main(string[] args)
        {
            String objstr1 = "SharePointBank.com";
            String objstr2 = "How2Sharepoint.com";

            if (objstr1 = objstr2)
            {
                Console.WriteLine("Equal.");
            }
            else
            {
                Console.WriteLine("Not Equal.");
            }               

            Console.ReadLine();
        }       
    }
}

Correct Program:

image

using System;

namespace MyApp
{
    class my_class
    {
        static void Main(string[] args)
        {
            String objstr1 = "SharePointBank.com";
            String objstr2 = "How2Sharepoint.com";

            if (objstr1 == objstr2)
            {
                Console.WriteLine("Equal.");
            }
            else
            {
                Console.WriteLine("Not Equal.");
            }               

            Console.ReadLine();
        }       
    }
}

No comments:

Post a Comment

Sponsored Ad

Development Updates