This post originated from an RSS feed registered with .NET Buzz
by Frans Bouma.
Original Post: VB.NET stupidity
Feed Title: Frans Bouma's blog
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/fbouma/Rss.aspx
Feed Description: Generator.CreateCoolTool();
Consider the following enum definition (which is defined in a C# assembly)
public enum EntityState:int
{
New,
Fetched,
OutOfSync,
Deleted
}
OK, now, we have an object which has a property called 'State' and this property is of type EntityState. All great, let's set that property State to EntityState.New in VB.NET:
myObject.State = EntityState.New
Does this statement compile or not? I give you a hint: when I press '.' after EntityState, the intellisense of VS.NET does understand it's an enum and gives the 4 values possible.
No! It does not compile! I even have defined Option Strict, but the VB.NET compiler thinks I'm calling Public Sub New() on EntityState even though I have omitted '()' and the compiler comes up with this brilliant knowledge: "Type '...EntityState' has no constructors'. Gee..., really? Perhaps that was the reason I didn't specify '()' so you might already have known it wasn't a method call, it was an enum value specification, dear compiler.
I have to specify:
myObject.State = EntityState.[New]
What a magnificant solution... Instead of fixing the syntax once and for all: always require '()' for a method call, they opt for a horrible workaround: "add [] around the name that confuses the compiler". Apparently even though the compiler knows it's an enum type and the value is a value of that enum type.