Solved

Extremely Challanging Question

Posted on 2004-05-02
17
187 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now