By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,065 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

Access the type from a shared method

P: n/a
Hi there,

I have a shared method in a base class, and I need to know which
subclass it has been called from. So for example

Public Mustinherit Class BaseClass

Public Shared Sub SharedMethod
...
End Sub

End Class

Public Class DerivedClass1 : Inherits BaseClass
....
End Class

Public Class DerivedClass2 : Inherits BaseClass
....
End Class
Now, if I call DerivedClass1.SharedMethod, or
DerivedClass2.SharedMethod how can I tell from within SharedMethod
which has been called? I don't even know if this is possible.

Thanks very much for any help,

Phil

Sep 12 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

You should not try to derive the type of class you are in the base
class and then do different things depending on that type , but instead
you should declare your base class method like Mustoverride without any
code

Public Mustinherit Class BaseClass

Public MustOverride Sub SharedMethod

End Class

and then in the inherited class you have to write an overriding
sharedmethod with code - the code can be different in the 2 derived
classes

Public Class DerivedClass1 : Inherits BaseClass
public overrides sub SharedMethod
'code for 1 here
end sub
End Class

Public Class DerivedClass2 : Inherits BaseClass
public overrides sub SharedMethod
'code for 2 here
end sub
End Class

I hope this helps.

wi******@yahoo.com schreef:
Hi there,

I have a shared method in a base class, and I need to know which
subclass it has been called from. So for example

Public Mustinherit Class BaseClass

Public Shared Sub SharedMethod
...
End Sub

End Class

Public Class DerivedClass1 : Inherits BaseClass
...
End Class

Public Class DerivedClass2 : Inherits BaseClass
...
End Class
Now, if I call DerivedClass1.SharedMethod, or
DerivedClass2.SharedMethod how can I tell from within SharedMethod
which has been called? I don't even know if this is possible.

Thanks very much for any help,

Phil
Sep 12 '06 #2

P: n/a
Well I could do that... I'm not trying to do different things in each
class though. A bit more detail perhaps.

I have some properties in the base class that I override in each sub
class. They just return string constants, but you can't override string
contants so they have to be properties. You can't declare properties as
shared and overrideable, so I can't access them from a shared method.
So what I have to do unfortunately is create an instance of the class
in order to get these string constants.

So I could write shared methods in each sub class to create an instance
and get the properties, then pass them to the shared method in the base
class. But that is messy to me, it's no cleaner than creating an
instance in my actual form code which is what I'm really trying to
avoid. They're 'one-hit' methods so I don't want to have to create an
instance. Maybe I'll just have to live with it.

Any ideas?
jandhondt wrote:
You should not try to derive the type of class you are in the base
class and then do different things depending on that type , but instead
you should declare your base class method like Mustoverride without any
code

Public Mustinherit Class BaseClass

Public MustOverride Sub SharedMethod

End Class

and then in the inherited class you have to write an overriding
sharedmethod with code - the code can be different in the 2 derived
classes

Public Class DerivedClass1 : Inherits BaseClass
public overrides sub SharedMethod
'code for 1 here
end sub
End Class

Public Class DerivedClass2 : Inherits BaseClass
public overrides sub SharedMethod
'code for 2 here
end sub
End Class

I hope this helps.

wi******@yahoo.com schreef:
Hi there,

I have a shared method in a base class, and I need to know which
subclass it has been called from. So for example

Public Mustinherit Class BaseClass

Public Shared Sub SharedMethod
...
End Sub

End Class

Public Class DerivedClass1 : Inherits BaseClass
...
End Class

Public Class DerivedClass2 : Inherits BaseClass
...
End Class
Now, if I call DerivedClass1.SharedMethod, or
DerivedClass2.SharedMethod how can I tell from within SharedMethod
which has been called? I don't even know if this is possible.

Thanks very much for any help,

Phil
Sep 12 '06 #3

P: n/a
<wi******@yahoo.comschrieb:
I have some properties in the base class that I override in each sub
class. They just return string constants, but you can't override string
contants so they have to be properties. You can't declare properties as
shared and overrideable
You can declare properties as 'Shared' or instance members + 'Overridable'.
Shared members cannot be overridden by a derived class.
so I can't access them from a shared method.
So what I have to do unfortunately is create an instance of the class
in order to get these string constants.
I am courious why you need a shared method at all if it is tied to
instances' properties.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Sep 12 '06 #4

P: n/a
Hi there,

Thanks for your response.

The only reason they are instance properties rather than shared is
because I need to be able to override them. They're really just string
constants. If I had my way the properties would be shared and
overrideable, the method would be shared, and there would be no
instances at all being created.

So to sum up, what I need is to have shared functionality in a base
class, with string contants being defined in subclasses. Ideally I
don't want to have to create instances at all. I want to be able to
call things like MySubClass.SharedFunctionName, and have the shared
function be able to access the string constants defined in the
subclass.

Is there a better way of doing what I'm trying to do?

Any help is greatly appreciated,

Phil

Herfried K. Wagner [MVP] wrote:
<wi******@yahoo.comschrieb:
I have some properties in the base class that I override in each sub
class. They just return string constants, but you can't override string
contants so they have to be properties. You can't declare properties as
shared and overrideable

You can declare properties as 'Shared' or instance members + 'Overridable'.
Shared members cannot be overridden by a derived class.
so I can't access them from a shared method.
So what I have to do unfortunately is create an instance of the class
in order to get these string constants.

I am courious why you need a shared method at all if it is tied to
instances' properties.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Sep 12 '06 #5

P: n/a
<wi******@yahoo.comschrieb:
The only reason they are instance properties rather than shared is
because I need to be able to override them. They're really just string
constants. If I had my way the properties would be shared and
overrideable, the method would be shared, and there would be no
instances at all being created.

So to sum up, what I need is to have shared functionality in a base
class, with string contants being defined in subclasses. Ideally I
don't want to have to create instances at all. I want to be able to
call things like MySubClass.SharedFunctionName, and have the shared
function be able to access the string constants defined in the
subclass.

Is there a better way of doing what I'm trying to do?
You may want to implement the Singleton design pattern in your classes in
order to create default instances. Then you can make the shared member an
overridable instance member too.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Sep 12 '06 #6

P: n/a
OK I like that, that's better. Now I can call things like
MySubClass.Def.FunctionName

rather than
New MySubClass().FunctionName

which is a lot nicer to my eye, and definitely more efficient if you're
doing it a lot.

Thanks very much for your help Herfried, you're a start :)

Phil

Herfried K. Wagner [MVP] wrote:
<wi******@yahoo.comschrieb:
The only reason they are instance properties rather than shared is
because I need to be able to override them. They're really just string
constants. If I had my way the properties would be shared and
overrideable, the method would be shared, and there would be no
instances at all being created.

So to sum up, what I need is to have shared functionality in a base
class, with string contants being defined in subclasses. Ideally I
don't want to have to create instances at all. I want to be able to
call things like MySubClass.SharedFunctionName, and have the shared
function be able to access the string constants defined in the
subclass.

Is there a better way of doing what I'm trying to do?

You may want to implement the Singleton design pattern in your classes in
order to create default instances. Then you can make the shared member an
overridable instance member too.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Sep 12 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.