|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Access the type from a shared methodI 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 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. wingp***@yahoo.com schreef: Show quoteHide quote > 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 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: Show quoteHide quote > 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. > > wingp***@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 <wingp***@yahoo.com> schrieb:
> I have some properties in the base class that I override in each sub You can declare properties as 'Shared' or instance members + 'Overridable'. > 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 Shared members cannot be overridden by a derived class. > so I can't access them from a shared method. I am courious why you need a shared method at all if it is tied to > So what I have to do unfortunately is create an instance of the class > in order to get these string constants. instances' properties. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/> 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: Show quoteHide quote > <wingp***@yahoo.com> schrieb: > > 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/> <wingp***@yahoo.com> schrieb:
Show quoteHide quote > The only reason they are instance properties rather than shared is You may want to implement the Singleton design pattern in your classes in > 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? 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/> 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: Show quoteHide quote > <wingp***@yahoo.com> schrieb: > > 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/> |
|||||||||||||||||||||||