Hi,

I am trying to define a 'null' LINQ Table as a global variable and then use a switch statement to deturmine what the Table should be.

I am having problems defining the datatype and also assigning values to it.

Hopefully the code below will explain better.

System.Data.Linq.Table table;

switch (x) {

case '1':
    table = DataClassesDataContext.Table_Name;
    break;
case '2':
    table = DataClassesDataContext.Table_Name2;
    break;
}

Thanks for any help given

System.Data.Linq.Table is a generic type, thus you can't create an object of this type. You must provide the 'parameter' so the compiler can complete the generic class and make it a specific class. For example, you can't have a List<>, you provide a type with it (List<int>) and the compiler 'completes' the class.

That given, if your two classes you wish to assign to this variable have a common base type you can create a Linq.Table of that type and assign them as you are doing. For example. if the classes are derived from "MyDatabase", you could use

System.Data.Linq.Table<MyDatabase> table = null;

Hi,

Thanks for the help. I don't know if it matters by im using LINQTOSQL so I have loaded my database into a .dbml layout and I am accessing the classes using:

DataClassesDataContext db = new DataClassesDataContext();

I added your code below:

DataClassesDataContext db = new DataClassesDataContext();
System.Data.Linq.Table<db> table = null;

And there is a problem with the 'db' on the Table<db> part.
(db is a Field but used like a Type)

Thanks.

db is a variable, not a type.
You can have a Table of DataClassesDataContext, not a Table of db.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.