开发者

this.Type reserved ? Should be reported

I discovered yesterday that I can't have a Class that uses a field named "Type" this is probably reserved.

Although the field may be reserved you still can't set anyObject.Type if it doesn't have a Type field defined as a public string. Ignoring any getters and setters and jumping directly to changing YourObject to "whatever" string.

Try this for yourself. Define a Type field and try setting it.

This should be reported to Microsoft so no one will use "Type" as a field in the future, there is no warnings/errors along trying to define it.

        public Point_Extended(Point p, booking b)
        {
            this.ID = p.ID;
            this.Type开发者_运维知识库 = p.Type;
            this.Status = p.Status;
            this.Size = p.Size;
            this.Coords = p.Coords;
            this.Color = p.Color;
            this.BuildingID = p.BuildingID;

            this.Login = b.Login;
            this.Starts = b.Starts;
            this.Hours = b.Hours;
            this.BookingID = b.ID;
        }


If there is a name abiguity - just use this.Type / obj.Type (instance), TypeName.Type (static) or System.Type (the type). Or in really nasty cases, global::System.Type. This works just fine and matches the question (I think):

static class Program
{
    static void Main() {
        Test anyObject = new Test();
        anyObject.Type = "abc";
    }
}
class Test
{
    public string Type;
}


You are defining "Type" in a scope local to your class, e.g.

class SomeClass
{
    public string Type { get; set; }
}

and then using it in some method of that class, e.g.

class SomeClass
{
    public string Type { get; set; }

    public void DoSomeStuff()
    {
        Type = "Foo";
    }
}

This is ambiguous between "Type" in SomeClass (a property) and "Type" in namespace System (a type).


Nevermind.

It was the ToString() override representation that confused me to think the Type was changed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜