
how to raise base class event and method from the derived child object
Nicholas,
I believe I misunderstood your question.
Are you asking how to get at the events and methods unique to a Car when you
have a Vehicle variable?
If you are you are losing "Polymorphism", which is what you are striving for
when you declare the variable as the base object!
You can use DirectCast or CType (DirectCast is preferred). to convert the
Vehicle variable into a Car variable.
DirectCast(objVehicle, Car).CarMethod(x, y)
Be certain that objVehicle is really a Car for this to work. You can use 'If
TypeOf objVehicle Is Car Then'
To handle an event that is unique to Car from the objVehicle variable, you
can use AddHandler & RemoveHandler
AddHandler objVehicle.NoFuel, AddressOf OutOfFuel
Where OutOfFuel is a sub that matches the signature of the event...
Hope this helps
Jay
Quote:
> It is true the derived class are able to raise the base class event by the
> method that you recommand. Somehow that is possble only if i directly
assign
> the CVihecle to the derived object like
> dim objVehicle as Car 'Car is object derived from CVehicle
> objVehicle = new Car
> but that method will not work if i
> dim objVehicle as CVehicle
> objVehicle = new Car
> Is there anyway i can access the event and method in the car which is not
> inherit from the CVehicle.
message
> > Nicholas,
> > You should follow the same pattern that C# & the .NET Framework uses.
> > Allowing derived classes to raise the event.
> > ' The Delegate is optional, useful if you have multiple
> > ' events with the same signature.
> > ' See System.EventHandler
> > Public Delegate Sub LowFuelHandler(ByVal sender As Object, _
> > ByVal e As EventArgs)
> > ' Both of these have the same signature!
> > Public Event LowFuel As LowFuelHandler
> > Public Event NoFuel As System.EventHandler
> > Public Event FuelFull As System.EventHandler
> > Protected Overridable Sub OnLowFuel(ByVal e As EventArgs)
> > RaiseEvent LowFuel(Me, e)
> > End Sub
> > Then in this class or any derived classes you would call OnLowFuel to
> raise
> > the event.
> > This allows derived classes to handle the event without using the
> 'Handles'
> > keyword, it also allows derived classes to extend or replace the
EventArgs
> > parameter to the event...
> > NOTE: When overriding the OnLowFuel method in a derived class, you need
to
> > be very certain to call MyBase.OnLowFuel so as to raise the event for
> other
> > handlers!
> > If you want to pass 'more' arguments to the event, derive a class from
> > EventArgs and use it as the second argument of the delegate above...
> > Hope this helps
> > Jay
> > > hi
> > > Recently for learning purpose, I was trying to write a set of object
> using
> > > the vb.net new inherits technic. Let say I have a base class call
> vehicle
> > > and there are three derived class from the vehicle class( car, lorry,
> > bike).
> > > Following is the contain in the Cvehicle class
> > > ------------------------
> > > Those property are
> > > mFuel(fuel leave)
> > > MaxFillLevel( maximum fuel level)
> > > Method
> > > Run
> > > Refuel
> > > Event
> > > LowFuel
> > > NoFuel
> > > FuelFull
> > > ---------------------
> > > I use a form to implement the program
> > > 1) I declare a global variable call objvehicle as CVehicle
> > > 2) There are some radio button which represent car, lorry and bike
> > > 3) Depend on which button user click, following assignment will be
> > > implement, objvehicle = new car or objvehicle = new lorry or
objvehicle
> =
> > > new Bike
> > > I handle those event by using private sub objvehicle_lowFuel(byVal
> Message
> > > as string) handle objvehicle_lowfuel
> > > The above method run fine until I try to add in an extra method and
> event
> > > specified for class lorry only (loadcargo and Cargo_Loaded).
> > > 1) By using the above CVehicle = new Lorry assignment, the new method
> > > Loadcargo and new event Cargo_Loaded can not be use because Cvehicle
> class
> > > doesn't not support those method
> > > 2) if I would like to use above method(assign object at run time) is
> there
> > > anyway I can convert the objvehicle to act like the Lorry class which
> > > support those method.
> > > Thank you
> > > Nicholas