Use of class function

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have a class named MyClass with 2 properties A and B and two
function Fa and Fb.

When I am using the class I am able to do the following:

Dim mc As MyClass
Dim s As String = mc.Fa

But I can't do:

Dim s As String = MyClass.Fa

Why?

Thanks,

Miguel
 
You can only do the latter, if the variable is a static (shared in vb.net)
variable.

public class MyClass

public shared MyString as string = "ABC"

end class

something like that.
 
Is Fa marked as shared? Any properties or functions you have defined on a
class are part of the object and can be accessed once you actually
instantiate a copy of the class. The only way to be able to access a
function without first instantiating a class is to mark it shared (or static
in C#). The downside of this, because this means you can access the function
without instantiating an object, you can't make use of anything within that
function that relies on the properties or variables of the class. So, if
MyClass.Fa was shared simply returned a pre-defined value or performed some
operation that didn't require the other parts of Fa that are not marked as
shared as well, then you can access it directly. If MyClass.Fa just returned
the results of 2 + 2 that could be workable (or accepted some parameters and
worked with those) but MyClass.Fa couldn't do something like add MyClass.A
and MyClass.B together since Fa isn't part of the instantiated object.
 
Back
Top