Solved

Extremely Challanging Question

Posted on 2004-05-02
17
191 Views
Last Modified: 2011-09-20
Hi,

I have a class which is inherited form many classes... Say ClassBase.

ClassDerive is inheriting ClassBase.

From a SHARED method in classbase, I would like to programatically see the name or type of the deriving class i.e. "ClassDerive"... I cannot use the gettype as it needs an instance or the name of the class (which is what I am looking for)...

Any help extremely appreciated.
0
Comment
Question by:igalnassi
  • 6
  • 5
  • 3
  • +1
17 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 10974060
Cannot be done, as the base class has no idea that it IS being used as the baseclass for ANY actually existing 'derived' class.

Just because you have a Class cAnimal does NOT in and of itself require that there be a Class cTiger that inherits from cAnimal.  And cAnimal has no way of finding out that cTiger (or any other cXXX exists, that inherits from cAnimal)

What are you trying to accomplish with this?

AW
0
 

Author Comment

by:igalnassi
ID: 10974129
I know that... I want the CTiger.SharedMethod(), which is an inherited to be able to understand it is in CTiger. I am not going to call CAnimal.SharedMethod() and then try to get CTiger...

Thanks anyway Arthur... Any more comments deeply appreciated and will be rewarded...
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10975200
I sat here and thought about this one for a while.

I hate to be the one to say this but there IS a way to do this ... its a pain in the butt but there is.

Before I go into how you can do this I feel it important to note that this is what instance members are for. Doing this within an instance member is trivial. I think I know a situation similar to what you are trying to do here. Create a Singleton base class. There are other patterns for this.

ok method 1 (easiest)
pass in a type to the method

public shared foo(Type _Type)
class.foo(typeof(class))

the second situation is much more complex and would involve using some stuff presented here http://msdn.microsoft.com/msdnmag/issues/02/11/CLRDebugging/default.aspx.

I dont think that is really what should be done and is no way safe or "good" code.

Greg
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:rehand
ID: 10980376
Hmm...

Why not create an Overridable function that provides this information.
0
 
LVL 4

Expert Comment

by:rehand
ID: 10980397
Public Class Class1
    Public Overridable Function WhoAmI() As String
        WhoAmI = "Class1"
    End Function
End Class

Public Class Class2
    Inherits Class1

    Public Overrides Function WhoAmI() As String
        WhoAmI = "Class2"
    End Function
End Class
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10980594
rehand: note that he said a static (aka shared) method. This means that he will not have access to instance members.
0
 
LVL 4

Expert Comment

by:rehand
ID: 10980757
This works too

Public Class BaseClass
    Public Function WhoAmI() As String
        WhoAmI = Me.GetType.ToString
    End Function
End Class

Public Class Class2
 Inherits BaseClass
End Class

Public Class Class2
 Inherits Class2
End Class

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As New Class3
        MsgBox(x.WhoAmI())
    End Sub
End Class

Message Box Returns: Class3
0
 
LVL 4

Expert Comment

by:rehand
ID: 10980774
Err the third declare class should have read Class2 instead of Class3 again:

Public Class BaseClass
    Public Function WhoAmI() As String
        WhoAmI = Me.GetType.ToString
    End Function
End Class

Public Class Class2
 Inherits BaseClass
End Class

Public Class Class3
 Inherits Class2
End Class

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As New Class3
        MsgBox(x.WhoAmI())
    End Sub
End Class

Message Box Returns: Class3
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10980897
again note SHARED/STATIC

doing it with an instance member is trivial thats what they are there for.
0
 

Author Comment

by:igalnassi
ID: 10983754
thanks gregoryyoung...

Basically I might just put a shared Type Property that is overriden on every deriving class. But still it would not solve the problem of reaching to it from the base class...

Can BaseClass.SharedFunction reach DerivedClass.InheritedSharedProperty which is overriden in the DerivedClass do you think?
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10983913
again note SHARED/STATIC

you can only override instance members ...

public shared virtual Foo() { } //gives error as virtual and shared are mutually exclusive
0
 
LVL 4

Expert Comment

by:rehand
ID: 10985872
gregory

I am not sure what you are eluding to with SHARED/STATIC.

I created a baseclass with the WhoAmI method, compiled it, and they create a ChildClass that inherits the base class. I then included the childclass in a new executable an called the WhoAmI procedure of the ChildClass which inherits and does not override the WhoAmI method from the baseclass and the application returned the value: ChildClass.

0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10988647
From the original question

"From a SHARED method in classbase, I would like to programatically see the name or type of the deriving class i.e. "ClassDerive"... I cannot use the gettype as it needs an instance or the name of the class (which is what I am looking for)..."

You can not override shared members ... Your point of doing it with instance members works only for other instance members not for shared/static members. It is a good example though of a point which I was tried to make earlier and may not have gotten accross. This is one reason why instance members were created, to solve this problem.

As for the solution the standard way of doing it is to pass in a type to the shared member.

example:
Base Instance(Type TypeToCreate) {
    try {
    Base ret = (Base) CreateInstance(TypeToCreate) ;
    }
    catch (Exception Ex) {
       throw new System.Exception("Type was not convertable to Base or not loadable", Ex) ;
    }
    return Base
}

or you can pass in an instance to work on ...
0
 

Author Comment

by:igalnassi
ID: 11098703
Thanks I have solved the problem by putting the type as an argument from the Deriving class.

Thanks for all your support anyway

Igal
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
ID: 11099705
umm I suggested that in my first post ...

"ok method 1 (easiest)
pass in a type to the method"
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question