Sponsored Ad

Tuesday, April 5, 2011

Inconsistent accessibility: property type 'MyApp.class_type' is less accessible than property 'MyApp.operator_error_class.my_prop' – C# Error

When you try to access a class from another class which have higher accessibility.

using System;

namespace MyApp
{
    public class Inconsistent_accessibility_Class
    {
        public static void Main(string[] args)
        {          
        }

        public class_type my_prop
        {
            get
            {
                return new class_type();
            }
        }

    }
    class class_type
    {
    }
}

How to fix above Error:

add public access in front of class_type class.

using System;

namespace MyApp
{
    public class Inconsistent_accessibility_Class
    {
        public static void Main(string[] args)
        {          
        }

        public class_type my_prop
        {
            get
            {
                return new class_type();
            }
        }

    }
    public class class_type
    {
    }
}

How to Fix C# Error: Operator '+' cannot be applied to operand of type 'string'

You will get this C# error (Operator '+' cannot be applied to operand of type 'string') when you try to apply binary + operator with only single operand. To fix this error, you need to have + operator between the two strings.

using System;

namespace MyApp
{
    class operator_error_class
    {
        public static void Main(string[] args)
        {

            string s = + "my string";
            Console.Read();
        }      
    }
}

Working version above program:

using System;

namespace MyApp
{
    class operator_error_class
    {
        public static void Main(string[] args)
        {

            string s = "Its" + "my string";
            Console.Read();
        }      
    }
}

How to Fix C# Error - Identifier too long

This error you will get when you try to declare a variable of length more than 512 chars. The below code will produce a compile time error: Identifier too long

using System;

namespace MyApp
{
    class too_long_class
    {
        public static void Main(string[] args)
        {
            int intsssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss = 0;
            Console.WriteLine();
            Console.Read();
        }      
    }
}

To fix above error just reduce the number of char in variable name less than 512.

How to fix C# Warning - The variable 'i' is assigned but its value is never used

While coding i got a warning saying that “The variable 'i' is assigned but its value is never used”. This warning basically appear because the given variable is not in use.

If you compile the below code it will give above warning.

using System;

namespace MyApp
{
    class _class
    {
        public static void Main(string[] args)
        {
            int i = 0;
            Console.Read();
        }
    }
}

How fix above warning :

To fix above warning just use the unused variable anywhere in the code.

using System;

namespace MyApp
{
    class _class
    {
        public static void Main(string[] args)
        {
            int i = 0;
            Console.WriteLine(i);
            Console.Read();
        }
    }
}

Can we Initialize a Class Object with Object Class in C#

Someone asked me a question that can we initialize a class object with object class. No it is not possible because every class is derived from object class. So we can not assign object class object to any other class. While reverse is possible.

If you try to compile below program. It will give error saying:

Cannot implicitly convert type 'object' to 'MyApp._class.my_class'. An explicit conversion exists (are you missing a cast?)

using System;

namespace MyApp
{
    class _class
    {
        public static void Main(string[] args)
        {

            my_class cObj = new object();           

            Console.Read();
        }

        class my_class
        {
            public my_class()
            {
            }
        }
    }
}

Friday, April 1, 2011

Efficient way to convert Array to String using C#

Here is way to convert a given char array into string. Just pass the array into string constructor and it will automatically convert into string.

Efficient way to convert Array to String using C#

using System;

namespace MyApp
{
    class array_to_string_class
    {
        static void Main()
        {
            char[] array_var = new char[16];
            array_var[0] = 'S';
            array_var[1] = 'o';
            array_var[2] = 'f';
            array_var[3] = 't';
            array_var[4] = 'w';
            array_var[5] = 'a';
            array_var[6] = 'r';
            array_var[7] = 'e';
            array_var[8] = ' ';
            array_var[9] = 'T';
            array_var[10] = 'e';
            array_var[11] = 's';
            array_var[12] = 't';
            array_var[13] = 'i';
            array_var[14] = 'n';
            array_var[15] = 'g';

            string str = new string(array_var);
            Console.WriteLine(str);

            Console.Read();
        }
    }
}

Sponsored Ad

Development Updates