469,572 Members | 1,395 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

Find out child type in base class call?

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

Jun 22 '06 #1
6 3471
I have a suggestion - how about defining your sandal class, and inherit
the shoe class?

T

ro*********@gmail.com wrote:
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

Jun 22 '06 #2
Roland,

You mean GetType?

http://msdn.microsoft.com/library/de...ttypetopic.asp

Cor

<ro*********@gmail.com> schreef in bericht
news:11*********************@m73g2000cwd.googlegro ups.com...
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

Jun 23 '06 #3
Hello, Roland,

Possibly something like:

Return Activator.CreateInstance(Me.GetType)

Cheers,
Randy
ro*********@gmail.com wrote:
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

Jun 23 '06 #4
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
<ro*********@gmail.com> wrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
| 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
|
Jun 23 '06 #5
Jay,

Good catch,

Cor

"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> schreef in
bericht news:Ox**************@TK2MSFTNGP02.phx.gbl...
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
<ro*********@gmail.com> wrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
| 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
|

Jun 23 '06 #6
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

Jay B. Harlow [MVP - Outlook] wrote:
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
<ro*********@gmail.com> wrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
| 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
|


Jun 23 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Suzanne Vogel | last post: by
9 posts views Thread by Martin Herbert Dietze | last post: by
3 posts views Thread by Maheshkumar.R | last post: by
2 posts views Thread by brianbender | last post: by
reply views Thread by Bruin | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.