views:

58

answers:

4

Can somone explain why I get this error on this code?

Error 1 The type 'ConsoleApplication1.TestClass' already contains a definition for 'IsThisOK'

class TestClass
    {
        public bool IsThisOK { get; set; }

        public static bool IsThisOK(string str)
        {
            return true;
        }

        public static void Test()
        {
            TestClass c = new TestClass();
            c.IsThisOK = IsThisOK("Hello");            
        }
    }
+2  A: 

You're trying to define a property and a method with the same name. While you can have multiple methods that override each other (with different argument lists), you cannot have a property and a method that share the same name

Zippit
This is the most close to an answer I am looking for. What I really want to know is why? after all it just creates 2 new methods and a variable(as seen in IL).
Randall Flagg
You can use method group syntax in C#, which would by ambiguous if properties and methods could have the same name. There are probably other examples. It's a C# issue, not IL.
Douglas
Thank you both :)
Randall Flagg
A: 

Because you cannot provide same name to a function and a Property. You cannot overload function with property. You can use it in this way:

 class TestClass

{
    public bool IsThisOK { get; set; }

    public static bool isThisOK(string str)
    {
        return true;
    }

    public static void Test()
    {
        TestClass c = new TestClass();
        c.IsThisOK = isThisOK("Hello");
    }

}

Rajesh Rolen- DotNet Developer
You shouldn't really be distinguishing two pubic members by case alone, not all of the languages on the CLR are case sensitive.
Douglas
@Douglas: but c# is case sensitive and questions is asked for c#
Rajesh Rolen- DotNet Developer
This would work, but I'm with Douglas in feeling that the distinction by case alone is potentially confusing. In this particular example, it might make sense to give the two slightly different names which might convey their distinct intent. Since a property is often treated as a status of sorts, it might make sense to call it something like "IsOK", whereas methods convey more of an intent to perform an operation (/make a determination?), so it might make sense to leave that as "IsThisOK"
Steven
@Steven:Thanks Steven, i agree with you and Douglas but i just tried to give solutions on basis of question and situation. thanks for explaining me. Thanks
Rajesh Rolen- DotNet Developer
"You shouldn't really be distinguishing two pubic members by case alone, not all of the languages on the CLR are case sensitive." Good point Douglas. Thanks you, And thank you all.
Randall Flagg
A: 

You've declared IsThisOK twice, at line 3 and line 5 (property and static function).

Try to imagine how could the compiler could figure out to which you are referring later on?

smirkingman
@smirkingman: IsThisOK is not declared twice.. its a property and a method.. he is trying to overload method with property which is not possible
Rajesh Rolen- DotNet Developer
A: 

As other pointed out, you cannot have a method and a property with the same name.

However, you can more or less work around this by using an extension method if you like:

static class TestClassExtension
{
    public static bool IsThisOK(this TestClass, string str)
    {
        return true;
    }
}

class TestClass
{
    public bool IsThisOK { get; set; }

    public static void Test()
    {
        TestClass c = new TestClass();
        c.IsThisOK = this.IsThisOK("Hello");
    }
}
Lucero
i dont think this gonna work.
Rajesh Rolen- DotNet Developer
@Lucero: there is error in code. Plez recheck or explain how it gonna work. Thanks
Rajesh Rolen- DotNet Developer