PC Review


Reply
Thread Tools Rate Thread

Declaring instance of class in subs?

 
 
Brett
Guest
Posts: n/a
 
      11th Mar 2005
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


 
Reply With Quote
 
 
 
 
rawCoder
Guest
Posts: n/a
 
      11th Mar 2005
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

"Brett" <(E-Mail Removed)> wrote in message
news:u8Yp%(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Brett
Guest
Posts: n/a
 
      11th Mar 2005

"rawCoder" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Morgan
Guest
Posts: n/a
 
      11th Mar 2005
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


"Brett" <(E-Mail Removed)> wrote in message
news:u8Yp%(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
rawCoder
Guest
Posts: n/a
 
      11th Mar 2005
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

"Brett" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "rawCoder" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
>
>



 
Reply With Quote
 
Brett
Guest
Posts: n/a
 
      11th Mar 2005
Thanks. That's what I needed to do.

Brett
"Morgan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
> "Brett" <(E-Mail Removed)> wrote in message
> news:u8Yp%(E-Mail Removed)...
>> 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
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a local variable once for all subs Poniente Microsoft Excel Programming 2 1st Oct 2011 04:30 AM
Declaring subs Public vs Private Rick Microsoft Excel Programming 9 19th Dec 2007 12:30 AM
Defining methods for a class that are accessible when declaring arrays of that class??? Bob Rock Microsoft Dot NET 7 3rd Jun 2004 02:21 PM
Defining methods for a class that are accessible when declaring arrays of that class??? Bob Rock Microsoft C# .NET 7 3rd Jun 2004 02:21 PM
Defining methods for a class that are accessible when declaring arrays of that class??? Bob Rock Microsoft Dot NET Framework 7 3rd Jun 2004 02:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:34 PM.