I am developing in C#.net and I believe I need to use generics for the problem I have however I don’t know where to start.
Here are the steps I need to do:
- Get ID from user – DONE
- Query database table using ID – DONE
- Pull back one row – DONE
- Create a object type dependant on ID
- Populate object dependant on type
Different appointments require different information being pulled back (populated) from the database. For example one appointment might only require the users forename/surname where another may require forename/surname/dob.
I want the child class to execute first and then call the parent method. The issue is myAppointment is still be treated as a Appointment object not a DoctorAppointment/OtherAppointment once it has gone through the Switch statement. Where myAppointment is declared I actually dont know what type of object is it going to be
Here is the code I currently have:
tblAppointment getAppointment = (from getApp in dc.tblAppointments
where getApp.appID == Id
select getApp).SingleOrDefault();
// Needs to be set the something. I really want this be to set to a generic type as I don’t really know what type it will be until it has been through the switch statement
Appointment myAppointment = null;
switch (getAppointment.tblAppointment.appTypeID)
{
case 2:
{
// Changes myAppointment from Appointment to DoctorsAppointment
myAppointment = new DoctorsAppointment();
}
break;
case 1:
{
myAppointment = new OtherAppointment();
}
break;
default:
break;
}
// I want this to set myAppointment to DoctorsAppointment.Populate(getAppointment)
return myAppointment.Populate(getAppointment);
Appointment is parent class.
DoctorsAppointment/OtherAppointment are child classes.
The Populate within the child classes require a parameter whereas the parent class doesn’t. The error I am currently recieveing is:
No overload for method 'Populate' takes '1' arguments
Hopefully I have explained myself enough.
Thanks,
Clare
Suggest you take one of the inheritance answers above
I thought it was worth asking if you are using LINQ to SQL? The kind of functionality you need is partially implemented for you if you have one table, that stores different kinds of objects.
See here for an example