المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Trying To Create A Data Access Layer



C# Programming
12-17-2009, 04:23 AM
and I have little idea how to implement it...

I created a code folder, and a DeviceData class in it, with a constructor that (hopefully) constructs a local device of a variable type when it's instantiated. I don't actually know if it's possible to pass an object to a constructor and cast it to a specific object type based upon a Type parameter, but the compiler doesn't whine about it. I assume it's working. But I could be wrong...

My thinking is to add to the DeviceData class member functions that handle the Add, Edit, Delete, and View functions that encapsulate all the nasty database communication details so the rest of my program doesn't have to deal with them. I just want to pass an object of one of 5 types to this code block, specify what I want it to do with it, and forget it.

The devil is always in the details, though, and when I try to buld a function to add a new device to the database, the IDE snivels about an Identifier being required as the Add function parameter. I know that doesn't help much, so here's the constructor:

public DeviceData(int Type, object Device)
{
//Create a local device instance of the proper type

switch (Type)
{
case 1: //Recloser
Recloser dev = new Recloser();
dev = (Recloser)Device;
break;
case 2:
//Regulator dev = new Regulator();
//dev = (Regulator)Device;
break;
case 3:
//Breaker dev = new Breaker();
//dev = (Breaker)Device;
break;
case 4:
//Transformer dev = new Transformer();
//dev= (Transformer)Device;
break;
case 5:
//LTC dev = new LTC();
//dev = (LTC)Device;
break;
default:
break;
}

}

I could be completely out to lunch, but I think that creates a member variable called dev of a type defined by the object type passed to it. At least, I hope so, and VS doesn't generate any nasty errors when I build the solution. Unfortunately, when I try to actually use the variable, things get ugly:

public int GetDevice(dev)
{
return 0;

}

At this point, the compiler complains that I haven't provided an identifier, and I don't know why. My intent is to instantiate the DeviceData object by passing the constructor a specific object that has been populated using a Form in another part of the program. My thinking is that this should create an instance variable, called dev, that can be filled with the field data from the passed object. The next step would be to query the database to populate the fields, then return the object to the calling Form module. I haven't a clue at this point why I'm receiving this message, though, and would appreciate any input.

"A Journey of a Thousand Rest Stops Begins with a Single Movement"