views:

161

answers:

2

Hi,

When you have a local variable named the same as a type, is there any way to tell the compiler that the symbol you have given is a type or a variable? For instance consider (and ignore all type return errors):

public class sometype { public static sometype DoSomething() {} }

public string sometype { get { return sometype.DoSomething(); } } //A
public string sometype { get { return sometype.Trim(); } } //B
public sometype sometype { get { return sometype.DoSomething(); } } //C
public sometype sometype { get { return sometype.Trim(); } } //D
  • A -> Error (no method DoSomething())
  • B -> Works
  • C -> Works
  • D -> error (no method Trim())

From a more applicative point of view

(you may want to skip this if XSD bores you):

I am currently trying to get LINQ to XSD working. In my XSD file there are xs:elements like this:

<xs:element name="car" type="car">

Where the 'car' type is defined as a simpleType like this
(probably some more restrictions but this is it in essence):

<xs:simpleType name="car">
 <xs:restriction base="xs:string" />
</xs:simpleType>

So naturally LINQ to XSD generates code that looks like this:

public string car {
    get {
        XElement x = this.GetElement(XName.Get("car", ""));
        return XTypedServices.ParseValue<string>(x, XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String).Datatype);
    }
    set {
        this.SetElementWithValidation(XName.Get("car", ""), value, "car", car.TypeDefinition);
    }
}

But this won't compile due to the aforementioned problem.

+5  A: 

You should fully qualify the namespace of the type.

If the type does not have a namespace, then you can prefix it with global:: (in C# anyway).

Drew Noakes
Genius, I must be too tired to be coding right now if that didn't enter my mind.
Graphain
Thanks for the global:: suggestion too.
Graphain
You're welcome. You'll see that some generated code tends to use the global prefix with a fully qualified namespace to avoid these kinds of collisions.
Drew Noakes
global:: happened to be required so thanks :-)
Graphain
+2  A: 

You can look into C# Specification to get more information on this behavior. Here is the start of the chapter that describes it:

7.3 Member lookup
A member lookup is the process whereby the meaning of a name in the context of a type is determined. A member lookup can occur as part of evaluating a simple-name (§7.5.2) or a member-access (§7.5.4) in an expression. If the simple-name or member-access occurs as the simple-expression of an invocation-expression (§7.5.5.1), the member is said to be invoked. If a member is a method or event, or if it is a constant, field or property of a delegate type (§15), then the member is said to be invocable. Member lookup considers not only the name of a member but also the number of type parameters the member has and whether the member is accessible. For the purposes of member lookup, generic methods and nested generic types have the number of type parameters indicated in their respective declarations and all other members have zero type parameters.

Dzmitry Huba
Thanks, I never really thought about the spec before but recent work has tended to push me to the limits of the language (yeah this stupid question betrays that :P) and the spec will probably come in handy.
Graphain