Question Casting an interface to an object

nicorvp

Member
Joined
May 3, 2009
Messages
24
Location
Italy
Programming Experience
5-10
Hi, I have a base class called Schedule and three classes inherited: DailySchedule, WeeklySchedule, MontlySchedule.
Now I would like to build a form in which users can modify the schedule and I have to compile different parts of it depending on whether the schedule is daily, weekly or montly. Then I want to have back the schedule modified.
Which object can I use on the form?
I thought to implement on the base class an interface iSchedule and to pass the interface to the form, but when I'm on the form constructor how can I cast the interface to the correct class? eg. in the form constructor I will have a sched variable as iSchedule, I can see that, for example, is a DailySchedule from an interface property called Type. But how can I cast the interface to an instance of the DailySchedule class and to access to its members?
I hope I was clear in exposing my question
 
TypeOf Operator (Visual Basic)
You don't need an interface when you already have a base class. Interfaces is used to bind different classes to a common member set, you then usually interact with the objects using the interface type. Using the base type as parameter you can pass any inherited object, and check and cast to true type if you need access to derived members.
 
Back
Top