Partager via


Why is my column named _Region in the Typed DataSet?

One quirk you'll find when making a typed DataSet with a column or table named "Region" is that the generated code produces a property called "_Region".

What's up with that? If you read Raymond Chen's blog, you already have a pretty good guess: Backward compatability.

In previous versions of Visual Studio, we did the same thing. Why did we do it back then? When we generated a typed DataSet, we ask the Code DOM provider (a generic way of producing semantically equivalent code in multiple languages) if a property name is a valid identifier. This is important so that when you have a column named "Class", we generate "_Class" and the DataSet still compiles. Before Visual Studio 2005, Code DOM provider told us that "Region" was not a valid identifier, so we helpfully fixed up the to be "_Region". The catch is, "Region" is a valid identifier in VB. In the latest version, the Code DOM was fixed to answer the question correctly.

All is good so far - a bug got fixed in the product, so everyone should be happier, right?. Until you migrate your VS 7.1 DataSet with a column called "Region" to VS 8.0 and regenerate the designer code. The result is that all of your code which referenced _Region is no longer valid. Life isn't so good now. In the end we decided that the best thing to do was special-case the name in the generator so that compatability would be maintained. Since there's no difference between "old" DataSets and "new" ones, people writing new code still get to see this (very minor and rare) oddity. Such is the price we pay for compatability

Bonus trivia: We detect this case for another name: "ExternalSource" also gets a free underscore in the DataSet generator.

- Ryan Cavanaugh

Comments

  • Anonymous
    November 10, 2005
    I think you also do it for _Module, but I haven't checked if Module is a valid identifier in VB.NET 8.
  • Anonymous
    May 17, 2006
    Hi,As the DataSetgenerator creates public properties that start with an underscore, VS.NET throws up warnings saying "'MS.IT.Ops.Hermes.DataTransferObjects.ComponentEnterpriseProducts._ComponentEnterpriseProducts' is not CLS-compliant".How do we suppress this warning then?thanks.