469,616 Members | 1,792 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,616 developers. It's quick & easy.

Access the type from a shared method

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
6 1361

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
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
<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
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
<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
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.

Similar topics

11 posts views Thread by Mike MacSween | last post: by
6 posts views Thread by Peter Frost | last post: by
5 posts views Thread by IcingDeath via DotNetMonster.com | last post: by
17 posts views Thread by rdemyan via AccessMonster.com | last post: by
6 posts views Thread by roland.bali | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.