Find out child type in base class call?

  • Thread starter Thread starter roland.bali
  • Start date Start date
R

roland.bali

Hi,

Here is the basic setup, my base class is Shoe which has a child class
called Sandal. I would like to create objects by calling Sandal.Load.
But without overloading Load in Sandal and sending the Sandal type to
the base class I can not instantiate a Sandal class from within Shoe.

Public Class Shoe
Public Shared Function Load() As Object
return ?????
End Function
End Class

Public Class Sandal
Inherits Shoe
End Class

Public Class MyShoeBox
Private mySandal as Sandal = Sandal.Load()
End Class

So, my question is: Is it possible to find out the type/class which was
actually called from within the base class?

Kind regards,
Roland
 
Hello, Roland,

Possibly something like:

Return Activator.CreateInstance(Me.GetType)

Cheers,
Randy
 
Roland,
| Public Shared Function Load() As Object
Ah! There's the rub!

The Load method is Shared, meaning it is part of the Shoe class, and not
inherited by the Sandal class per se.

Although VB allows you to code:

Sandal.Load()

It calls Shoe.Load directly in the IL, which means that Shoe.Load will never
know you attempted to call it on the Sandal class.

| So, my question is: Is it possible to find out the type/class which was
| actually called from within the base class?
If Load was an instance method (not shared) you could use Me.GetType within
it to get the actual type of the object (the derived type).
Seeing as Load is a shared method, there is no such method sans passing a
parameter.


I would consider:
1) making the Load method Generic
2) passing a Type parameter
3) adding Load to each derived type, instead of in the base class
4) create Shoe & Sandal Factory classes that allow polymorphism via
inheritance...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hi,
|
| Here is the basic setup, my base class is Shoe which has a child class
| called Sandal. I would like to create objects by calling Sandal.Load.
| But without overloading Load in Sandal and sending the Sandal type to
| the base class I can not instantiate a Sandal class from within Shoe.
|
| Public Class Shoe
| Public Shared Function Load() As Object
| return ?????
| End Function
| End Class
|
| Public Class Sandal
| Inherits Shoe
| End Class
|
| Public Class MyShoeBox
| Private mySandal as Sandal = Sandal.Load()
| End Class
|
| So, my question is: Is it possible to find out the type/class which was
| actually called from within the base class?
|
| Kind regards,
| Roland
|
 
Hi Jay,

Thanks for Your answer (and to all You other guys too for that matter).
Great explanation of what's happening.

Before I wrote my question I used your second suggestion of using a
Type parameter to the Load method. All class inheriting from the Shoe
class shadowed the Load method and sent GetType(Sandal) (for the Sandal
class) as parameter to Shoe.Load.

This works like a charm but it didn´t seem like a good looking
solution since all classes inheriting Shoe whould need to be modified
if I made some changes later on. Anyway I guess I stick to that for
now.

After giving it some thought I think the way I want to solve things
violates object oriented thinking since it's a kind of backward object
orientation.

Kind regards,
Roland
 
Back
Top