473,749 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bug? Scope of private variables in classes

One odd thing I've come across - if you declare a private variable in
a class, it is exposed to other instances of that same class.

To replicate this behaviour, create a class like this:

Public Class Class1

Private mintID As Integer = 0

Public Sub New(ByVal pintID As Integer)
MyBase.New()
mintID = pintID
End Sub

Public Sub Compare(ByVal pobj As Class1)
If (mintID = pobj.mintID) Then
MessageBox.Show ("IDs are equal!")
Else
MessageBox.Show ("IDs are not equal!")
End If
End Sub

End Class

then create a form with one button, and put this code into the Click
event procedure:

Dim obj1 As New Class1(5)
Dim obj2 As New Class1(4)
Dim obj3 As New Class1(5)

obj1.Compare(ob j2)
obj1.Compare(ob j3)

When you run the application, you will see two messages: "IDs are not
equal!", then "IDs are equal!". So, it is able to access the private
variables, and it is also looking at the other instance (rather than
retrieving the value from itself).

(I'm using Visual Studio 2003, with v1.1 of the .NET Framework)

I'm not sure whether this is intentional, or a bug - the documentation
seems a bit vague on this point. Personally, I can see where it would
be useful (I came across it when I was investigating deep vs shallow
copying of objects), but I'd prefer the private variables to be
hidden. This reminds me of the way that Friend access works in C++
(rather than VB), where you can explicitly say "Class X has access to
my hidden properties".

John
Nov 20 '05 #1
3 1365
No this is correct, Private members of a Class are avauilable to other
instances of the same class. However, you can Shadow this if you inhert the
class .

Regards - OHM
"John C Kirk" <jk***@pmsi-consulting.com> wrote in message
news:79******** *************** ***@posting.goo gle.com...
One odd thing I've come across - if you declare a private variable in
a class, it is exposed to other instances of that same class.

To replicate this behaviour, create a class like this:

Public Class Class1

Private mintID As Integer = 0

Public Sub New(ByVal pintID As Integer)
MyBase.New()
mintID = pintID
End Sub

Public Sub Compare(ByVal pobj As Class1)
If (mintID = pobj.mintID) Then
MessageBox.Show ("IDs are equal!")
Else
MessageBox.Show ("IDs are not equal!")
End If
End Sub

End Class

then create a form with one button, and put this code into the Click
event procedure:

Dim obj1 As New Class1(5)
Dim obj2 As New Class1(4)
Dim obj3 As New Class1(5)

obj1.Compare(ob j2)
obj1.Compare(ob j3)

When you run the application, you will see two messages: "IDs are not
equal!", then "IDs are equal!". So, it is able to access the private
variables, and it is also looking at the other instance (rather than
retrieving the value from itself).

(I'm using Visual Studio 2003, with v1.1 of the .NET Framework)

I'm not sure whether this is intentional, or a bug - the documentation
seems a bit vague on this point. Personally, I can see where it would
be useful (I came across it when I was investigating deep vs shallow
copying of objects), but I'd prefer the private variables to be
hidden. This reminds me of the way that Friend access works in C++
(rather than VB), where you can explicitly say "Class X has access to
my hidden properties".

John

Nov 20 '05 #2
Actually scrub what I just said, thats not true about shadowing, that wont
help.

OHM

"One Handed Man ( OHM#)" <news.microsoft .com> wrote in message
news:Of******** ******@TK2MSFTN GP10.phx.gbl...
No this is correct, Private members of a Class are avauilable to other
instances of the same class. However, you can Shadow this if you inhert the class .

Regards - OHM
"John C Kirk" <jk***@pmsi-consulting.com> wrote in message
news:79******** *************** ***@posting.goo gle.com...
One odd thing I've come across - if you declare a private variable in
a class, it is exposed to other instances of that same class.

To replicate this behaviour, create a class like this:

Public Class Class1

Private mintID As Integer = 0

Public Sub New(ByVal pintID As Integer)
MyBase.New()
mintID = pintID
End Sub

Public Sub Compare(ByVal pobj As Class1)
If (mintID = pobj.mintID) Then
MessageBox.Show ("IDs are equal!")
Else
MessageBox.Show ("IDs are not equal!")
End If
End Sub

End Class

then create a form with one button, and put this code into the Click
event procedure:

Dim obj1 As New Class1(5)
Dim obj2 As New Class1(4)
Dim obj3 As New Class1(5)

obj1.Compare(ob j2)
obj1.Compare(ob j3)

When you run the application, you will see two messages: "IDs are not
equal!", then "IDs are equal!". So, it is able to access the private
variables, and it is also looking at the other instance (rather than
retrieving the value from itself).

(I'm using Visual Studio 2003, with v1.1 of the .NET Framework)

I'm not sure whether this is intentional, or a bug - the documentation
seems a bit vague on this point. Personally, I can see where it would
be useful (I came across it when I was investigating deep vs shallow
copying of objects), but I'd prefer the private variables to be
hidden. This reminds me of the way that Friend access works in C++
(rather than VB), where you can explicitly say "Class X has access to
my hidden properties".

John


Nov 20 '05 #3
* jk***@pmsi-consulting.com (John C Kirk) scripsit:
One odd thing I've come across - if you declare a private variable in
a class, it is exposed to other instances of that same class.

To replicate this behaviour, create a class like this:

Public Class Class1

Private mintID As Integer = 0

Public Sub New(ByVal pintID As Integer)
MyBase.New()
mintID = pintID
End Sub

Public Sub Compare(ByVal pobj As Class1)
If (mintID = pobj.mintID) Then
MessageBox.Show ("IDs are equal!")
Else
MessageBox.Show ("IDs are not equal!")
End If
End Sub

End Class

then create a form with one button, and put this code into the Click
event procedure:

Dim obj1 As New Class1(5)
Dim obj2 As New Class1(4)
Dim obj3 As New Class1(5)

obj1.Compare(ob j2)
obj1.Compare(ob j3)

When you run the application, you will see two messages: "IDs are not
equal!", then "IDs are equal!". So, it is able to access the private
variables, and it is also looking at the other instance (rather than
retrieving the value from itself).
That's what I would expect:

<msdn>
The Private keyword in the Dim statement declares elements to be
accessible only from within the same module, class, or structure.
</msdn>
I'm not sure whether this is intentional, or a bug - the documentation
seems a bit vague on this point. Personally, I can see where it would
be useful (I came across it when I was investigating deep vs shallow
copying of objects), but I'd prefer the private variables to be
hidden. This reminds me of the way that Friend access works in C++
(rather than VB), where you can explicitly say "Class X has access to
my hidden properties".


It's the /same class/ in the VB case.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4

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

Similar topics

6
2217
by: Hal Vaughan | last post by:
Being self taught, this is one thing I've always had trouble with -- I finally get it straight in one situation and I find I'm not sure about another. I have a class that keeps calling an internal method. If I define variables within the method, I would expect that they're new and clean each time I call the method. If I'm calling from within the class, is that true? Or should I re-initialize the variables each time the method is...
3
3020
by: Neil Zanella | last post by:
Hello, It seems to me that using too many variables at class scope in C++ (e.g. private data members) can be just as bad as having a C program with lots of global variables. This is especially true for large classes with lots of methods. The worse part is that even if conventions are used to assign names to class data members to so as to distinguish them from other variables, then it still becomes difficult to tell from the class...
9
2451
by: Steven T. Hatton | last post by:
It was once suggested to me that I could accomplish much the same thing that modules would accomplish (if C++ had modules) by writing my entire program - except for main() - inside of a class. When it was suggested, I didn't take it very seriously, but I have recently begun wondering if it is an idea worth considering. I'm now trying to think of what fundamental differences might exist between namespace scope, and class scope. One that...
4
2419
by: Marc Tanner | last post by:
Hello, I am currently working on a eventhandling system or something similar, and have the problem of loosing scope. I have read many interesting posts on this group and the faq article about closure, but it seems that i have still not understood everything. Below is my attempt to preserve the scope but it's not really nice and i think with the use of closure could it be done better. But at the moment i am quite confused and hope that...
10
3488
by: Not Available | last post by:
On the host server: namespace JCart.Common public class JCartConfiguration : IConfigurationSectionHandler private static String dbConnectionString; public static String ConnectionString { get { return dbConnectionString;
7
2115
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9 ----------------------------------------------------------------------------- This is a consortium of ideas from another thread on topic ----------------------------------------------------------------------------- One of the big issues of organizing items within a class, is there are many...
6
1674
by: Florian Lindner | last post by:
Hello, I have these class declarations class CBauteil { private: int m_iNumber; int m_iWeight; public: int getNumber(); void setNumber(int);
7
1418
by: surfrat_ | last post by:
Hi, My project has about 4 source files (xxx.cs) and I am having a problem with scope between the files. If I put the code all within one class everything works OK. Can you please point me to some good resources which explain scope on the file/class level. What I want to do is as follows: File A containing class AA File B containing class BB
1
25695
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that variable can only be used in that function. If you were to try to access that variable anywhere else in...
0
35245
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For the sake of brevity I am sticking to common usage. Wherever the term procedure is used in this tutorial it actually refers to a subroutine or function. Definition of Scope The scope of a variable where this variable can be seen or accessed...
0
8996
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
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8256
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...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
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.