473,796 Members | 2,704 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Declaring instance of class in subs?

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
Nov 21 '05 #1
5 3152
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" <no@spam.com> wrote in message
news:u8******** ********@TK2MSF TNGP14.phx.gbl. ..
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

Nov 21 '05 #2

"rawCoder" <ra******@hotma il.com> wrote in message
news:ex******** ******@TK2MSFTN GP09.phx.gbl...
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
Nov 21 '05 #3
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" <no@spam.com> wrote in message
news:u8******** ********@TK2MSF TNGP14.phx.gbl. ..
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

Nov 21 '05 #4
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" <no@spam.com> wrote in message
news:eo******** ******@TK2MSFTN GP15.phx.gbl...

"rawCoder" <ra******@hotma il.com> wrote in message
news:ex******** ******@TK2MSFTN GP09.phx.gbl...
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

Nov 21 '05 #5
Thanks. That's what I needed to do.

Brett
"Morgan" <ab**@senditon. com> wrote in message
news:ON******** ******@TK2MSFTN GP09.phx.gbl...
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" <no@spam.com> wrote in message
news:u8******** ********@TK2MSF TNGP14.phx.gbl. ..
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


Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1730
by: Bob Rock | last post by:
Hello, this may seem a strange question, but is there a way of being able to call methods of a class through an array of that class when not referencing a specific object in the array. In other words, defined a class class_A I'd like to be able to do the following: // defining an array of class_A objects class_A myArray = new class_A;
6
1881
by: Steve Jorgensen | last post by:
Many of the regulars here have explained that declaring variables using As New .... is a bad idea, and some have given some good explanations, but I wanted add one more demonstration to the mix. This example is less practical than some, but more illustrative than most. I came up with this to show a fellow programmer why I think he's using As New ... too much, and I though I might as well share it here, too. First create a class as...
4
6300
by: Lucy | last post by:
In the Declarations section of a form's code module, what is the difference between the following: Dim Flag As Boolean Public Flag As Boolean Private Flag As Boolean Thanks!
20
4263
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the interface and accessing the datastore MUST pass in a UserToken in the constructor of the object. Is this not possible? Am I forced to add the UserToken as a property on the object instead? /Ole
4
1582
by: Ant | last post by:
Hi I'm quite new to C#. I've been playing around with variables declared outside the scope of a method with & without the static keyword. What difference does declaring variables in either fashion make? For that matter, why must you declare methods with the static keyword? Thanks for any ideas Ant
2
1292
by: Marc Robitaille | last post by:
Hello, I created a VB class that inhertis from System.Web.UI.Page. I set the class to be MustInherit because I set some subs and functions to be MustOverride. I created a webform call frmClient. By default, the page inherits from frmClient class. In the frmClient class, by default, it inherits from System.Web.UI.Page. So, I changed this to my MustInherit class. I implemented the subs and functions that my MustInherit class needs to be...
15
2972
by: CR | last post by:
I've noticed that the trend these days is to declare variables in the middle of code instead of at the top. What is the advantage of this? It seems like it makes it hard to reuse variables. Here is how all the examples I've seen so far create an OleDbCommand Object: Dim cmd as new OleDbCommand("Select * FROM Table1",cnn) I had to figure out that it was the same as this:
7
9299
by: Iain Mcleod | last post by:
Hi This must be an often encountered problem. I want to declare an abstract class or an interface with nothing but several static constants so that I can use polymorphism when I call each of them from my code. My stab at the problem is shown below. Can anyone suggest what my most efficient workable solution would be (i.e. I don't want to have to create instances of the classes as they will only store constant information and I would...
8
11441
by: kevin | last post by:
I have a form and in the form I have a sub that uses a class I instantiate using visual basic code: Public oCP As New Rs232 'instantiate the comm port I need to share this sub with another form so to declare the sub I use visual basic code: Public Shared Function IsPortAvailable(ByVal ComPort As Integer) As Boolean
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10244
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.