473,320 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Maybe as stupid question

I have an arraylist. In specific intervals of time I want to get the
data from this array list, clear it, and start putting new data in it.
But I run into problem.
Me code:

Dim ar1 as New ArrayList
' Code to add items to ar1
Dim ar2 as New ArrayList
ar2 = ar1
ar1.Clear
At this point the ar2 also is cleared. Why, and how to prevent?

Nov 21 '05 #1
4 853
"Nikolay Petrov" <jo******@mail.bg> schrieb:
I have an arraylist. In specific intervals of time I want to get the
data from this array list, clear it, and start putting new data in it.
But I run into problem.
Me code:

Dim ar1 as New ArrayList
' Code to add items to ar1
Dim ar2 as New ArrayList
ar2 = ar1
This assignment will make 'ar1' /and/ 'ar2' point to the same instance of
'ArrayList'. Notice that 'As New' for 'ar2' does not make sense because the
instance that is created will be released to the Garbage Collector for
collection after assigning 'ar1' to 'ar2'. The important point is that
variables of a reference type ('ArrayList' is a reference type) "are" not
objects, but /point/ to objects. Multiple variables can point to the same
object.
ar1.Clear
At this point the ar2 also is cleared. Why, and how to prevent?


Use the code below instead:

\\\
Dim al1 As New ArrayList
al1.Add("Bla")
al1.Add("Goo")
Dim al2 As ArrayList = al1.Clone()
al1.Clear()
MsgBox(CStr(al2.Count))
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #2
"Nikolay Petrov" <jo******@mail.bg> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Dim ar1 as New ArrayList
Dim ar2 as New ArrayList
ar2 = ar1


ArrayLists are Objects, so whenever you create a variable
"containing" one, (like ar1 and ar2, above), you are actually create a
(Object Reference) variable that (dare I say it) "points" to the /actual/
object. Assigning one object reference variable to another simply copies
the "Pointer", not the actual object (which might be *huge*).

You'll need to copy all the values from one ArrayList into the other;
there's probably a built-in method to do it ...

HTH,
Phill W.
Nov 21 '05 #3
After the statement 'ar2 = ar1', both variables reference the same object.

Instead. try:

ar2.AddRange( ar1.ToArray( ) )

"Nikolay Petrov" <jo******@mail.bg> schreef in bericht
news:11*********************@f14g2000cwb.googlegro ups.com...
I have an arraylist. In specific intervals of time I want to get the
data from this array list, clear it, and start putting new data in it.
But I run into problem.
Me code:

Dim ar1 as New ArrayList
' Code to add items to ar1
Dim ar2 as New ArrayList
ar2 = ar1
ar1.Clear
At this point the ar2 also is cleared. Why, and how to prevent?

Nov 21 '05 #4
Thanks guys.

The credit goes to Herfried again ;-)

Nov 21 '05 #5

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

Similar topics

4
by: Julian Campbell | last post by:
I have for a few weeks now been looking to improove my computer skills. I have been looking at learning VB but would like to know what exactly I can use it for!!! ( I said it may be stupid ) I...
8
by: sebb | last post by:
I'm kind of newbie to programming, but I thought of something and I want some opinions on that. It's about a new instruction block to do some cycles. I thought about that because it's not very...
119
by: rhat | last post by:
I heard that beta 2 now makes ASP.NET xhtml compliant. Can anyone shed some light on what this will change and it will break stuff as converting HTML to XHTML pages DO break things. see,...
5
by: raz | last post by:
Greetings all. I apologize for what is almost certainly a stupid question, but I can't figure this out, and have no more time for head bashing... The short version: what is the appropriate...
4
by: Mok-Kong Shen | last post by:
Apology, if this is OT. (Please kindly refer me eventually to another group.) With VC++ I can embed asm statements in a function e.g. as follows: __asm { ........ je lab
2
by: Ron Weldy | last post by:
I read that you don't need .cs files to deploy but I suppose if you are trying to reconstruct someone's work, you will need these files. Is that correct?
6
by: Adam Smith | last post by:
I have posted this and similar questions repeatedly and can't even raise a single response. I am being led to believe that this then 'Must be a stupid question' although people say that there is no...
2
by: SR | last post by:
What group would be best for posting a question about ASP.NET 2.0 using Master/Content pages? I am trying to assign a style sheet to a content page and I have no idea how to do so... TIA
2
by: Lynx101 | last post by:
Hi, Is this a stupid question? Senario: Two tables linked together with an ID number. Question: When using a combo box, which refences another table by indexed autonumber, is there a way to...
9
by: Alec | last post by:
Sorry guys, stupid question.... Am no programming expert and have only just started using php for creating dynamic news pages. Then I see a dynamic website without the php extension. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.