Use of class function

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
 
S

sloan

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

Mark Fitzpatrick

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,100
Latest member
MelodeeFaj
Top