|
-
Sep 23rd, 2004, 08:48 AM
#1
Thread Starter
Fanatic Member
Help with foreach [Resoloved]
Last edited by Bombdrop; Sep 24th, 2004 at 04:02 AM.
Useful Links
.Net
#Develop, GhostDoc, CodeKeep , be.PINVOKE, Good Code Snippet Site
Krypton Toolkit, XPCC / XP Common Controls, QSS Windows Forms Components
VB.COM
VB.Classic Help File, MB Controls, MZTools, ADO Stored Procedure Generator add-in,
-
Sep 23rd, 2004, 10:19 AM
#2
Addicted Member
I think what's happening here is, when you add the new object to your list, you are not adding all the data, per se, you are only adding a pointer to the object. So let's say the object resides in memory location 103. Each time you 'Add', you are really adding a bunch of '103s' to your list. Now when you change elements of this object residing in location 103, all your pointers are just pointing to the same object. To resolve this you would have to make sure you are creating a new object before you add it to the list. There are several ways you can do this. I notice in your code you have a couple lines //p = new Person() commented out. If you uncomment them, does it then work correctly?
cudabean
-
Sep 23rd, 2004, 10:21 AM
#3
Thread Starter
Fanatic Member
It works if i uncomment the and creat a new instance of Person each time but is it possible to use a simmalar method with out making a new object.
Useful Links
.Net
#Develop, GhostDoc, CodeKeep , be.PINVOKE, Good Code Snippet Site
Krypton Toolkit, XPCC / XP Common Controls, QSS Windows Forms Components
VB.COM
VB.Classic Help File, MB Controls, MZTools, ADO Stored Procedure Generator add-in,
-
Sep 23rd, 2004, 11:13 AM
#4
No. A Cudabean explained Arraylist is holding memory pointers not actual values. Using new assigns a new memory address.
This isnt VB6.
-
Sep 24th, 2004, 04:01 AM
#5
Thread Starter
Fanatic Member
Useful Links
.Net
#Develop, GhostDoc, CodeKeep , be.PINVOKE, Good Code Snippet Site
Krypton Toolkit, XPCC / XP Common Controls, QSS Windows Forms Components
VB.COM
VB.Classic Help File, MB Controls, MZTools, ADO Stored Procedure Generator add-in,
-
Sep 24th, 2004, 12:01 PM
#6
Addicted Member
It works if i uncomment the and creat a new instance of Person each time but is it possible to use a simmalar method with out making a new object.
Yes, it's entirely possible. I think YOU would have to write the Add method. Your add method would have to issue the 'new' to allocate the space for your object. (In fact, I see you have done this in the previous post) However, I think it gets you in a good mindset to NOT do this, because most of the built-in functions in C# (and Java as well) are designed where you are in charge of instantiating your objects. Plus, if methods started appearing like the one you have just written, programmers would have to start becoming more vigilant and check if the particular Method requires objects to be pre-instantiated, or if it does it for you. This causes confusion and makes the programming more error-prone.
cudabean
Last edited by Cudabean; Sep 24th, 2004 at 12:07 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|