473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 3608
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an...
20
by: modemer | last post by:
Question is as in subject. For example: class BaseClass { public: void func() { do something; } // I don't want this function being overloaded in its inherited class };
9
by: Martin Herbert Dietze | last post by:
Hello, I would like to implement a callback mechanism in which a child class registers some methods with particular signatures which would then be called in a parent class method. In...
4
by: james | last post by:
I have a custom UserControl, which can have many sub class levels derived from it. I want to be able to discover all the components at Load time, but the only components I can see from the base...
3
by: Maheshkumar.R | last post by:
Hi groups, How i can command over the MDI CHIlD forms created dynamically at runtime from PARENT. Let say, i have generated 5 mdichild forms, but i want to work with child form1 from MDI...
2
by: brianbender | last post by:
I am trying to load and unload assemblies dynamically and call methods and properties when loaded into an Appdomain I can load assemblies all day in the current AppDomain without references and...
6
by: Michael | last post by:
Scenario: I have a base class that I want to enumerate the custom attributes of a field defined in the child of the class. Is this possible? I've tried various versions of BindingFlags on...
10
by: Peter Oliphant | last post by:
Is there a way of defining a method in a base class such that derived classes will call their own version, EVEN if the derived instance is referred to by a pointer to the base class? Note that the...
0
by: Bruin | last post by:
Hi All, I'm having a problem with MDI child forms when the reference to the MDI Parent is set in a Control library. (Sorry for the long post) I have an control library assembly which holds all...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.