473,657 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

= operator between collections

Hello.
Here is the situation I am facing.

I have 2 collections, A and B. Both have various elements (string type) in
them.
I want to reset them, empty all elements from these collections. I do this
by typing :

Dim Empty as new Collection
A=Empty
B=Empty

At first this works ok and both A and B are empty.

But...

when I add an element to collection B it is also added to collection A, eg

B.add ("Testing element")

will produce as a result A.Count=1 instead of 0.

Why is this happening?
Is it because with the commands A=x, B=x both A and B refer to the same
position of memory that holds values of x ?

Please help and if possible clarify this.

Thanks in advance
Vasilis.
Nov 21 '05 #1
3 945
Vasilis X schrieb:
Hello.
Here is the situation I am facing.

I have 2 collections, A and B. Both have various elements (string type) in
them.
I want to reset them, empty all elements from these collections. I do this
by typing :

Dim Empty as new Collection
A=Empty
B=Empty

At first this works ok and both A and B are empty.

But...

when I add an element to collection B it is also added to collection A, eg

B.add ("Testing element")

will produce as a result A.Count=1 instead of 0.

Why is this happening?
Is it because with the commands A=x, B=x both A and B refer to the same
position of memory that holds values of x ?


Right. Empty points to a new (and empty) collection. By assigning 'empty' to
'A' and to 'B' you copy the content of 'empty' into both variables. As the
content of 'empty' is a reference to the collection, now also A and B point
to the same collection. If you want to have A and B point to different
collections, you must create different collections:

a = new collection
b = new collection
A collection is a "reference type" which means that variables of that type
contain only the reference (= the position of the object in memory). In
opposite, there are "value types", like Integer, Double and Structure types.
Copying these variables also means copying the variable content, but this is
the object itself, not the reference to the object.

Armin
Nov 21 '05 #2
Dim Empty As New Collection, will create one new instance of a Collection
object. You are assigning a reference to Empty to both A and B.
Effectively they are "pointing" at the same instance (in C++ speak).

If, however, you write:

A = New Collection
B = New Collection

or

Dim Empty1 As New Collection, Empty2 As New Collection

A = Empty1
B = Empty2

then you have created two instances of a collection, one for each of A and
B.

Hope this makes sense :)


"Vasilis X" <so*****@somewh ere.com> wrote in message
news:O7******** ******@TK2MSFTN GP14.phx.gbl...
Hello.
Here is the situation I am facing.

I have 2 collections, A and B. Both have various elements (string type) in
them.
I want to reset them, empty all elements from these collections. I do this
by typing :

Dim Empty as new Collection
A=Empty
B=Empty

At first this works ok and both A and B are empty.

But...

when I add an element to collection B it is also added to collection A, eg

B.add ("Testing element")

will produce as a result A.Count=1 instead of 0.

Why is this happening?
Is it because with the commands A=x, B=x both A and B refer to the same
position of memory that holds values of x ?

Please help and if possible clarify this.

Thanks in advance
Vasilis.

Nov 21 '05 #3
CT
The Collection data type is a reference type and stored on the managed heap.
Reference type variables are not intrinsically tied to a fixed memory
location that always has a value like value type variables (Integer, String,
etc.). You can access the value of a reference type via a reference to the
memory location holding that value. This is because, the variable does not
hold the bits of the object, but refers to the bits of the object located in
the memory.

So, to cut a long story short, you've created an instance of the Collection
class and made two variables reference the collection. Well, you actually
have three references to the same object, Empty, A, and B.

--
Carsten Thomsen
Enterprise Development with VS .NET, UML, AND MSF
http://www.apress.com/book/bookDisplay.html?bID=105
Communities - http://community.integratedsolutions.dk

"Vasilis X" <so*****@somewh ere.com> wrote in message
news:O7******** ******@TK2MSFTN GP14.phx.gbl...
Hello.
Here is the situation I am facing.

I have 2 collections, A and B. Both have various elements (string type) in
them.
I want to reset them, empty all elements from these collections. I do this
by typing :

Dim Empty as new Collection
A=Empty
B=Empty

At first this works ok and both A and B are empty.

But...

when I add an element to collection B it is also added to collection A, eg

B.add ("Testing element")

will produce as a result A.Count=1 instead of 0.

Why is this happening?
Is it because with the commands A=x, B=x both A and B refer to the same
position of memory that holds values of x ?

Please help and if possible clarify this.

Thanks in advance
Vasilis.

Nov 21 '05 #4

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

Similar topics

30
10420
by: | last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated) before. In general when I have a C++ question I look for answers in "The C++ Programming Language, Third Edition" by Stroustrup. However, I've come upon a question that I can neither answer from "The Book" or a Google search (so yes, at least I RTFBed). I'm hoping that someone in this news group might know the answer. Overloading the Operator Say I want to develop a...
6
1771
by: gg | last post by:
If I have the following class - template <class T> class X { public: void dump(ostream & os) { // dump obj_ to os } private: T obj_; };
3
1667
by: Sam | last post by:
I want to overload the + operator for ArrayList for adding elements public static int operator +(ArrayList l, Object o) return (l.Add(o)) First I have discovered that the return value cannot be void and if I change it to something like int, I have to always write like this int i = list + 3; // I don't need The compiler doesn’t accept jus
1
1984
by: Ana Lindt | last post by:
Hello, I remember reading in one of my books that it was possible to access some collection's elements with the dot (.) operator just like if they were properties. The problem is I don't remember what this kind of operation was called thus making it difficult to find documentation about it. I would apreciate if someone could just tell me the name of this operation (of implementing collections with the dot operator) or the name of the...
3
2497
by: enahar | last post by:
Dear Sir/Madam, I have 2 lists as following: ApproverListBE appList1; ApproverListBE appList2 ; I want to add the above 2 lists.How do I override the + operator.
5
9942
by: Richard Lewis Haggard | last post by:
What is the syntax to over ride the array index operator ''? I want to derive a class from CollectionBase and have it encapsulate an array of another class I'm working with. Among other things, it needs to over ride the operator but I don't seem to be able to get the syntax quite right. Can someone tell me what the syntax should be in order to over ride the operator? -- Richard Lewis Haggard
12
2198
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid overloading of == and != but instead translate each call of objA==objB automatically in System.Object.Equals(objA, objB). This would remove inconsistencies like myString1==myString2
6
1286
by: JohnGoogle | last post by:
Hi all, For my own educational process I am implementing a Fraction class described in the VSJ magazine to help me understand Visual C# operator overloading. I'm a relative newbie. At the moment my basic code is: using System;
0
2050
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the increment operator in a class and then defining a derived class and using the derived class. When I try to use the increment operator on an instance of the derived class (as an instance of the derived class, not when used as an instance of the base...
0
8399
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
8312
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
8827
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...
0
8732
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...
1
8504
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
7337
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
4159
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...
1
2732
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
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.