views:

77

answers:

3

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;
        }
+4  A: 

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;
}
Marc Gravell
Hello Marc thanks for your reply.I updated the first post with my constructor in my class.I already use this.Type and p.Type is a string "rect" ..Using your example doesn't change the actual type of the Test class to "abc", so why wouldn't it work on my class ?Thanks alot!
dezza
@dezza - unless you show the entire class, I have no way of reproducing this. For example, what is `Point`? does *that* have a `.Type`? Can you post a **reproducible** example please?
Marc Gravell
I'm sorry .. I thought you only needed info about the .Type setting .. No need to be holding hidden cards ..http://pastebin.com/Z07pv9rV
dezza
@dezza - OK, I grabbed that from pastebin, fixed up all the typos (`Point` vs `point` etc) errors, invented `Exceptions` and `dbDataContext`, and it compiled. Cannot reproduce.
Marc Gravell
A: 

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).

jscharf
Should it also be ambiguous when using this.Type ? Using Marc's sample it works fine, but when using my own it still changes the type.
dezza
A: 

Nevermind.

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

dezza