tags:

views:

130

answers:

6

Hello all,

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:

  1. Get ID from user – DONE
  2. Query database table using ID - DONE
  3. Pull back one row - DONE
  4. Create a object type dependant on ID
  5. 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

+4  A: 

Hi, one solution is to make in parent class Appointment method like this

public virtual void Populate( getAppointment) 
{
   // does nothing
}

And then in child classes

public override void Populate( getAppointment) 
{
// do what you need
}

Best Regards, Iordan

IordanTanev
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.
ClareBear
Is `getAppointment` missing its type? or am I misreading your code?
Svish
getAppointment is being set via a LINQ object. tblAppointment getAppointment = (from getApp in dc.tblAppointments where getApp.appID == Id select getApp).SingleOrDefault();
ClareBear
+2  A: 

Sounds like a case for inheritance

abstract class Appointment
{
    public abstract void Populate(AppointmentData data);
}

class DoctorsAppointment : Appointment
{
    public override void Populate(AppointmentData data)
    {
        // implementation specific to DoctorsAppointment
    }
}
class OtherAppointment : Appointment
{
    public override void Populate(AppointmentData data)
    {
        // implementation specific to OtherAppointment
    }
}
Patrick McDonald
I think you mean 'abstract', not 'virtual' ...because virtual methods must have a body, and thus the above code won't compile
Andreas Grech
thanks, typed into this before debugging, should be fixed now
Patrick McDonald
A: 

You should alter Appointment's Populate method to be abstract (possibly virtual) and take whatever the type of getAppointment is. The subclasses DoctorsAppointment and OtherAppointment implement Popuplate by overriding the default from Appointment.

A: 
abstract class Appointment 
{
    public abstract string Populate(AppointmentData d);
}

class OtherAppointment : Appointment
{
    public override string Populate(AppointmentData d)
    {

    }
}

Make Appointment an abstract class to it cannot be instantiated by itself and make the populate method abstract as well so it must be implemented by it's inherited classes

Andreas Grech
+1  A: 

The Appointment class should declare the appropriate Populate method. You could also setup an Appointment factory class:

static class AppointmentFactory
{
    public static Appointment Create(int type) 
    {
        switch (type)
        {
            case 2:
                return new DoctorsAppointment();
            case 1:
                return new OtherAppointment();
            default:
                throw new AppointmentTypeNotSupportedException();
        }
    }
}

class Appointment
{
    public virtual void Populate(getAppointment data) { }
}

class DoctorsAppointment : Appointment
{
    public override void Populate(getAppointment data) {  }
}

class OtherAppointment : Appointment
{
    public override void Populate(getAppointment data) {  }
}
bruno conde
+2  A: 

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

MattH
I am using LINQ to SQL but I am unsure about how Guy Burstein has implemented it. Do I need to use XML base mapping?
ClareBear
The class diagram shown in the article, is this within the dbml?
ClareBear
Yes. Simply put, you would normally add the DBML file. Then drag and drop objects from Server explorer into it. This would then generate classes. In your cases, where you have one database table that maps to an inheritance hierarchy, you will need to manually do some adaptation to show the DBML how your hierarchy works
MattH