Forum Moderators: open
So how about starting with:
When and why do you have to use New in a dim statement? As in:
Dim x as Integer
vs.
Dim xpen As New Pen?
This declares x as an variable Integer.
DIM xpen AS NEW Pen
There is no implicit object creation in .NET. If an object variable contains Nothing when it is encountered, it is left unchanged and no instance is automatically created. In VB 6.0 the above line would have initialized xpen to Nothing as object class Pen.
The long hand (vs. short hand of the above) is:
DIM xpen AS Pen = NEW Pen
So, an instance of the Pen class is created as soon as the DIM statement is executed, and xpen is initialized to a reference to the new object.
Makes sense, doesn't it?
Some classes have constructors that take arguments. So you could say
DIM xpen AS Pen = NEW Pen('blue',3.4)
or short hand
DIM xpen AS NEW Pen('blue',3.4)
This would allow your object to have the default value instead of Nothing.
what's the difference between a structure, an array and an arraylist. When and why would you use each?
An array is a set of numbered items maintained contiguously in memory. An array has a fixed size. So if you want to add an additional item to an array, a new array must be created and all the items copied from the old array into the new one. In VB.NET that is done with the redim statement. This operation is not particularly fast.
An arraylist is similar to an array, except that memory management of a growing list is done for you. If you allocate an arraylist of 35 items, .NET actually allocates space for 64. When you add additional items to the list, it fills up to 64 without any additional allocation of memory. If you add a 65th item, it automatically allocates a new arraylist with 128 spaces and copies the 64 items from the old arraylist into the new one, then deallocates the memory for the old one (which gets cleaned up on the next garbage collection).
So when you have a growing list, it means that you don't have to manage the memory management issues of efficiently maintaining the size of the list. There is some overhead to the arraylist, so if you know the final size that you want the list to be ahead of time, you are better off using an array. If you don't know the size it needs to be, the arraylist is the better option.