Gary Gary - 1 month ago 12
C# Question

Visual Studio 2010 designer generating invalid code

This is a first for me, I thought I was completely losing my mind. I have a simple winform application to which I add a datagridview. I set the datasource for the dgv, using the same source I've used in half a dozen other projects, and configure the grid the way I want it. There is nothing else on the form at this point except for the grid, and the project now contains a data set, a binding source, and a table adapter. Great. However, the code generated by the designer in doing this is invalid and is causing compile errors, telling me that the data set table adapters object and the data set itself does not exist.

If I go into the designer where the compile error is I see the following lines:

this.tILEDataSet = new ImageEdit.TILEDataSet();
this.logosTableAdapter = new ImageEdit.TILEDataSetTableAdapters.LogosTableAdapter();


ImageEdit is the class to which I have added this bound control. If I remove the "ImageEdit.", the code compiles and works perfectly. Of course, since this is a designer generated file, as soon as I make any other changes, it reverts back to the code in question.

I did this three times, with three different projects, twice starting from scratch with a fresh instance of visual studio.

What could be going wrong to cause this to happen, and is there a fix, other than manually editing a designer file, which I never like to do?

I did not change anything, everything was generated by the designer. Both the namespace and class were created as ImageEdit. The designer contains the following definitions:
private TILEDataSet tILEDataSet;
private TILEDataSetTableAdapters.LogosTableAdapter logosTableAdapter;

The designer class definition does derive from global::System.ComponentModel.Component:

public partial class LogosTableAdapter : global::System.ComponentModel.Component {
.
.
.


Thanks again for any insight.

Answer

I suspect you have several members in your solution named ImageEdit, whether they be namespaces, classes, or other members. In Microsoft's Guidelines for Names, and in particular, Names of Namespaces they recommend:

Do prefix namespace names with a company name to prevent namespaces from different companies from having the same name and prefix.

Do not use the same name for a namespace and a type in that namespace. For example, do not use Debug for a namespace name and also provide a class named Debug in the same namespace. Several compilers require such types to be fully qualified.

Do not give the same name to types in namespaces within a single application model.
For example, if you were writing a library of special controls to be used by Windows forms application developers, you should not introduce a type named Checkbox because a type with this name already exists for the application model (CheckBox).

The second point alone should fix your issue. Using the first point, a company name as part of the namespace, will cause the code generator to provide a more concise name for your objects, such as:

this.tILEDataSet = new MyCompany.ImageEdit.TILEDataSet();
this.logosTableAdapter = new MyCompany.ImageEdit.TILEDataSetTableAdapters.LogosTableAdapter();