Extremely Challanging Question

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.
igalnassiAsked:
Who is Participating?
 
gregoryyoungConnect With a Mentor Commented:
umm I suggested that in my first post ...

"ok method 1 (easiest)
pass in a type to the method"
0
 
Arthur_WoodCommented:
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
 
igalnassiAuthor Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
gregoryyoungCommented:
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
 
rehandCommented:
Hmm...

Why not create an Overridable function that provides this information.
0
 
rehandCommented:
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
 
gregoryyoungCommented:
rehand: note that he said a static (aka shared) method. This means that he will not have access to instance members.
0
 
rehandCommented:
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
 
rehandCommented:
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
 
gregoryyoungCommented:
again note SHARED/STATIC

doing it with an instance member is trivial thats what they are there for.
0
 
igalnassiAuthor Commented:
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
 
gregoryyoungCommented:
again note SHARED/STATIC

you can only override instance members ...

public shared virtual Foo() { } //gives error as virtual and shared are mutually exclusive
0
 
rehandCommented:
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
 
gregoryyoungCommented:
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
 
igalnassiAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.