Declaring instance of class in subs?

B

Brett

In a class, I have several Private subs. I declare an instance of the class
such as:
Dim MySelf as new Class1

within a private sub. The motive is to provide access to other subs within
the same class. Is this correct?

It would be nice to declare the MySelf instance in the Class public space
(just under Private Class Class1). That will give this error:
Cannot refer to an instance member of a class from within a shared
method or shared member initializer without an explicit instance of the
class.

If I declare
Private Class Class1
outside of a sub or function, isn't that an instance of the class? Why
can't the other subs use this (refer to above error).

What is the best method of declaring an instance of a class for subs within
that class to have access to the instance?

Thanks,
Brett
 
R

rawCoder

If I took your question correctly.

In case you have no 'shared' sub , you can easily declare a variable of any
class on class level and access it in any sub.

Public Class Class1
Private x as Class2
Private Sub MySub()
x.foo()
End Sub
End Class
'( NOTE: Uncompiled Code)

Another option would be to have all the subs that want to access some
internal data be declared 'shared' alongwith the variable as well. This
needs to be done when the variable is already shared.

Public Class Class1
Private shared x as Class2
Private shared Sub MySub()
x.foo()
End Sub
End Class
'( NOTE: Uncompiled Code)

If the sub is shared and the variable is not then the sub cannot access the
variable as it needs some instance of the class (which it is not part of).

HTH
rawCoder
 
B

Brett

rawCoder said:
If I took your question correctly.

In case you have no 'shared' sub , you can easily declare a variable of
any
class on class level and access it in any sub.

Public Class Class1
Private x as Class2
Private Sub MySub()
x.foo()
End Sub
End Class
'( NOTE: Uncompiled Code)

This code will not work, it will throw the error I mentioned earlier. Also,
foo() is in Class1, not Class2. My scenario looks more like this;

Public Class Class1
Private x as Class1
Private Sub MySub()
x.foo() 'Error occurs here
End Sub
End Class

Thanks,
Brett
 
M

Morgan

Not sure why you would need to do this. If a method is marked as Private, it
can only be seen from within the class it is defined. You need not create
an instance of the object in order to access private methods from inside of
the object that contains the method.

Class1

Private Sub Test(Msg as string)
Msgbox (msg)
End Sub

Private Sub TestTheTest()
Test("Testing my private routine")
End Sub
 
R

rawCoder

I assume you are saying that foo is private.
Why do you want to call a method for a particular instance especially when
its private?
Ofcourse you wont be able to call it as the method being called is private
And is not accessible via instances of the class.
Instead you can call it directly without the instance variable.

HTH
rawCoder
 
B

Brett

Thanks. That's what I needed to do.

Brett
Morgan said:
Not sure why you would need to do this. If a method is marked as Private,
it can only be seen from within the class it is defined. You need not
create an instance of the object in order to access private methods from
inside of the object that contains the method.

Class1

Private Sub Test(Msg as string)
Msgbox (msg)
End Sub

Private Sub TestTheTest()
Test("Testing my private routine")
End Sub
 

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