ClareBear
ClareBear

Reputation: 1523

Incorporating Generics

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

Upvotes: 2

Views: 196

Answers (6)

IordanTanev
IordanTanev

Reputation: 6240

One solution is to, in parent class Appointment, make method like this:

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

And then in child classes:

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

Upvotes: 4

MattH
MattH

Reputation: 4227

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

Upvotes: 2

bruno conde
bruno conde

Reputation: 48265

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) {  }
}

Upvotes: 1

Andreas Grech
Andreas Grech

Reputation: 107950

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

Upvotes: 0

Reputation:

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.

Upvotes: 0

Patrick McDonald
Patrick McDonald

Reputation: 65431

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
    }
}

Upvotes: 2

Related Questions