new instance of class

T

Tomas

Hello,

I have created ToolsClass public class in my application and I refer to
class methods like this
ToolsClass.MyMethod. If I put this class in assemblly VS give error that
cannot call instance member "MyMethod" without an instance reference.

So I must use code below. Maybe someone could explain why it is so? Do I
need to free created instance?


abc:=new ToolsClass;
abc.MyMethod

Regards,
Tomas
 
P

Pavel Minaev

You can use the static (or Shared in VB) keyword to create a class method
rather than an instance method...

For VB, if all methods of the class are Shared, then it should
probably be a Module (which is really the same thing under the hood,
but slightly more convenient to work with).
 
T

Tomas

I have created ToolsClass public class in my application and I refer to
That's just how the type works. It has members that require an instance
of the type to access and "MyMethod" happens to be one of them. Trying to
access an instance member without an instance is like trying to get the
Nth element of an array without having an actual array.

What I do not understand is that I can use class methods without any
instance if that class is in
for example master web page of asp.net application. I just refer to method
like this ToolClass.MyMethod
directlly to class without creating any reference.
If I put that class in assemblly, then I get error about new instance and I
need to create new insance of the class.
 
T

Tomas

I see, yes if I declarate my class as static, I don't need to create class
instance anymore.
As I understand static classes is not thread safe, right?
 
T

Tomas

I see, yes if I declarate my class as static, I don't need to create class
instance anymore.
As I understand static classes is not thread safe, right?
 

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

Top