Last post Apr 08, 2008 01:14 PM by nikki_doer_of_things
Apr 07, 2008 07:12 PM|WebMasterSam.Net|LINK
My problem is not as simple as it looks like. Let's check the code here :
Private Class ClassNameWanted
Private Class BaseClass
Public Shared Function AnyMethod() As String
Return WHAT ????
As you can see, the method is static. I have to get the name of the class "ClassNameWanted" within the function "AnyMethod" in the base class.Everything I do always give me "BaseClass", not "ClassNameWanted".
I know it can be done, it HAS to be possible, but...
Apr 07, 2008 10:38 PM|nikki_doer_of_things|LINK
Why would it have to be possible? It's static. You have to invoke it using a class to begin with. Pass a type if you need it.
Public Class TestClass
Public MyData As Integer
Public Class SuperClass
Public Shared Function SharedMethod(ByVal T As Type) As String
TestClass.SharedMethod(GetType(TestClass)) will output TestClass
Seems a bit redundant, though. I wonder why you need it; perhaps you should be using an interface instead? This kind of sounds like factory methods, but those would override as required.
Apr 08, 2008 12:19 PM|WebMasterSam.Net|LINK
Your answer is what I decided to do last night to solve my problem. What I don't understand is
why the hell is it different because it is static ??
The problem I got is I have a business layer where everything is static. I have many classes like "SupportRequestManager", "MemberManager", "ForumCategoriesManager", "ForumSubjectsManager" who has methods like "Add", "Delete" and "Modify". Every "manager"
has a base class "ManagerBase" that has a function to get labels from an xml file name after the derived manager class, like "SupportRequestManager.xml". I have the same thing that works for my aspx pages. I have a base class for everypage and from the base
class I can get the name of the derived type to get the xml file.
Apr 08, 2008 01:14 PM|nikki_doer_of_things|LINK
Static methods don't require an instance of a class, so there is no real way to get the type of the class that invoked them, since there isn't one.
I would not design the classes so interdependently, as it might bite me later. I'd have an abstract method called GetXml() in the base class that returned an XmlDocument. All child classes would override it to get their data as appropriate. That XmlDocument
would be passed to the static method when it gets invoked. Sure, it's more work at first, but most of it would be copy/paste and it would likely save me a headache (and hack or redesign) later.