Structures and Arraylists
I was curious about the following scenario.
If I am understanding correctly, a structure is a value type, and a class is a reference type. This means from prior discussions I have had that a structure can be directly copied so that the copy is truly independent of the original:
Code:
CopyOfMyStructure = MyStructure
However, a copy of a class is NOT independent and simply vectors to the identical address of the original.
Now, If I am not mistaken, an arraylist is a class object. So if I copy a bunch of structures (value type) into an arraylist (reference type), do the structures that were copied in, get changed to reference type since they are now inside the arraylist?
Re: Structures and Arraylists
Think of each item in an ArrayList as a variable. Adding a value type object to an ArrayList is basically just like assigning it to a variable. If you then assign that item to another variable, the value gets copied, just as you would expect for a value type.
Re: Structures and Arraylists
But what if you copy the WHOLE arraylist?
vb Code:
CopyOfMyArraylist = MyArrayList
Re: Structures and Arraylists
That isn't copying the ArrayList. That is assigning the contents of one variable to another. If the variable is a value type then you are copying the value, so a new object is created. If the variable is a reference type, which ArrayList is, then you are copying the reference, so you now have two references to the same object. If there's only one object then there's obviously only one set of items.
Re: Structures and Arraylists
OK, that's what I figured. My use of the word "copy" was not correct, but "assign" is certainly what I was really meaning. And in my tests on a program, this is all becoming very evident. I can "copy" a structure from one arraylist to another, but I cannot "copy" one arraylist of structures to another arraylist without doing it item by item in a loop.
I hope this is right, anyway! :)
Thanks for the help!
Re: Structures and Arraylists
By the way, you really shouldn't be using ArrayLists unless you are working with .NET 2003 or earlier. Instead, you should be using the List.
It may help to consider what the variable holds when talking about reference types versus value types. In all cases, there is a chunk of memory that holds the actual object. A reference type variable holds the address of the actual object, while a value type variable holds the object itself. In that way, it is all consistent. When you use the = operator, it is always copying what is in the right side into what is in the left side (as long as you are using it for assignment rather than comparison, of course). However, if you are talking about a reference type, then what is in the right side is just the address, whereas if you are talking about a value type, then what is in the right side is the actual object. In both cases, the action taken is consistent, it is just that they are holding different things.
Re: Structures and Arraylists
Quote:
Instead, you should be using the List.
Generic list, as in List (Of T)?
Re: Structures and Arraylists
Quote:
Originally Posted by
treddie
Generic list, as in List (Of T)?
That's correct. The List(Of T) is the direct generic replacement for the ArrayList, providing dynamic array functionality. The List is preferable because it provides type-safety, e.g. only Strings can be added to a List(Of String), saves you having to cast upon retrieval and, very importantly, avoids boxing and unboxing of value types.
Re: Structures and Arraylists
What if you use List (Of Object)? Could you then have mixed types inside the list?
Re: Structures and Arraylists
Quote:
Originally Posted by
treddie
What if you use List (Of Object)? Could you then have mixed types inside the list?
As the name suggests, a List(Of Object) can contain anything that is an Object, which means object at all. It behaves pretty much like an ArrayList. It's relatively rare that you'd need to store any object at all in a collection though. If you do need to store different types then, if possible, use a common base type or inreface as the generic type of the List. Only use Object if you really must.
Re: Structures and Arraylists
Cool, thanks!
In addition, do you have a recommendation on what you think in your opinion is the best book on vb.net?
I have one that is OK, which is "Beginning Visual Basic 2005" (Willis/Newsome). It's not as thorough as I would like, though.
Re: Structures and Arraylists
It's very difficult to say what's best; especially for those of us who haven't been beginners for a long time. I can vouch for the 2008 version of Visual Basic Hot To Program from Deitel & Deitel being good as I reviewed it before it was published. I haven't seen the 2010 version but I would assume that it's of the same standard.
Re: Structures and Arraylists
I'll try your recommendation. I appreciate all of you guys helping me out, but I need to start understanding some serious underlying principles more, so I can come to these forums informed...and on the same page.