tags:

views:

1039

answers:

5

If you can't use Me. in a non-instance method, how would you use properties/functions from a base class in a Shared method?

For example, ClassB inherits from ClassA
ClassB has a Shared Method DoWork(); ClassA has a ReadOnly Property SecurityKey

How do I …

Public Sub DoWork()
  Dim test as String = Me.SecurityKey 
End Sub
A: 

If you're in a static context, you would need to instantiate this class to access its instance members.

baretta
+2  A: 

You access shared members by the classname and not an instance identifier.. IE: ClassA.DoWork() You cannot access instance members from shared methods/members because they are different scope. However you can access shared members from instance scope without using the instance identifier.

Try passing an instance of the class to your method or instantiating the class in the shared method.

Public Shared Sub DoWork(instance As ClassB)
    Dim test As String = instance.WhateverProperty;
End Sub
Quintin Robinson
I noticed if I mark my property shared in ClassA I can use it... but still can't use the prefix "Me." Is there a prefix/keyword to use? Some visual cue/hint that this available because of a class I inherited from?
tyndall
No, you can use the class explorer to help you see the modifiers different members of your class have, but you will not be able to access Shared/static members of an object with an instance identifier like Me/this. The best way to remember is if it is marked Shared then it is always ClassName.XXX
Quintin Robinson
+2  A: 

If you can't use "Me." in a non-instance method, how would you use properties/functions from a base class in a Shared method?

You can't. Shared methods (and static in C#) don't work well together with OOP. This (i.e. the fact that static/shared methods cannot be virtual/overridable) is arguably a design flaw in the .NET system that was “inherited” from Java. Shared methods aren't actually object methods, rather they are global methods with a name scope.

Additionally, and perhaps even more related to your problem, you always need an instance to access a non-shared method (which, being shared, does not belong to any particular instance).

Konrad Rudolph
Unfortunately, I'm trying to help some fix a VB.NET mess written by a completely non-OOP guy. And I'm trying to bolt in some OOP to clean up the mess. I think the person was using Shared methods from his "Class B"s to work with Object DataSources in ASP.NET... which I wouldn't have used those either
tyndall
but hey.... I'm trying to clean up someone else's problem without rewriting the whole thing. The person thought they were writing business objects and they are more like "table gateway" classes.
tyndall
+1  A: 

Using your examples:

Which ClassA instance should the shared ClassB.DoWork() get when it tries to reference the Me.SecurityKey property?

If you make the ClassA.SecurityKey property also shared that would make a little more sense, but then the inheritance relationship is no longer important. You might just as well say ClassA.SecurityKey inside that method, because you would have to already know about ClassA to know about it's inherited property anyway.

If you make ClassB.DoWork() as an instance method rather than a shared method, you can use the MyBase keyword in VB.Net to reference the inherited ClassA property, even if ClassB overloads or shadows it.

Joel Coehoorn
+1. Good points. And I think it was MyBase keyword I was thinking of.
tyndall
A: 
Dim s as SecKey = ClassA.SecurityKey

This pulls from a shared method. Remember that shared methods are shared by all instances of a class, so you use the class name to pull them.

Gregory A Beamer