Groups | Blog | Home
all groups > asp.net > february 2007 >

asp.net : questions about arrays and collections


Gilbert
2/24/2007 7:36:56 PM
H,

i'm starting with asp.net/vb.net and have some questions about arrays and
collections:

1) what's the difference between:
dim x() as string
and
dim x as array

2) can variable 'x' in the second case contain anything (string, integer
.... together)?

3) what is the correct syntax?
dim x as arraylist
or
dim x as arraylist()

4) what to choose between:
dim x as array (or if no difference dim x() )
and
dim x as arraylist

5) what to choose between:
dim x as arraylist
and
dim x as list(of string)


Thanks for helping me.
Gilbert


Göran_Andersson
2/24/2007 9:44:03 PM
[quoted text, click to view]

The difference is that the first one is a reference to a string array,
while the second one is a reference to any kind of array.

[quoted text, click to view]

Yes, and no. The reference can be used for any kind of array. If you
assign it an array of strings, you can only put strings in that array.
If you on the other hand assigns it an array of Object, you can put
anything in the array.

[quoted text, click to view]

That depends on what you want to do. The first one declares a reference
to an ArrayList, the second one declares a reference to an array of
ArrayList objects.

[quoted text, click to view]

Again, that depends on what you want to do. The first one declares a
reference to an array (of any type). The second one declares a reference
to an ArrayList.

An ArrayList is good if you want a list that grows dynamically. An array
can not be resized.

If you are using frameword 2.0, you can use a generi list instead of an
ArrayList (unless you want to mix data types in the list). Generics has
made ArrayList almost obsolete.

[quoted text, click to view]

Guess what? It depends on what you want to do. ;)

An ArrayList is equivalent to a List(Of Object). If you want a list
where you can mix data types, that is what you can use. If you only want
to put strings in the list, you should definitely choose the second one.


--
Göran Andersson
_____
Gilbert
2/24/2007 10:34:49 PM
Thanks for your explanation.
If you don't mind, 2 more questions ...

1)dim x as string() = dim x() as string = an array of string ?

2) dim x as array() = an array of array? Is this the same as dim x( , )?



"Göran Andersson" <guffa@guffa.com> schreef in bericht
news:%23rZqRSFWHHA.5092@TK2MSFTNGP03.phx.gbl...
[quoted text, click to view]

Cor Ligthert [MVP]
2/25/2007 12:00:00 AM

[quoted text, click to view]
Yes

[quoted text, click to view]
dim a as object = x(0)(0) gives in the first situation the first one.

Göran_Andersson
2/25/2007 12:00:00 AM
[quoted text, click to view]

Yes, they are the same.

The first one is the new syntax, where being an array is considered to
be part of the data type. The second one is the old syntax from when
arrays was a special kind of variables.

(Actually the Dim command was originally only used for arrays. Regular
variables was not declared at all.)

[quoted text, click to view]

No, it's not the same. An array or arrays is also called a jagged array.

In a jagged array you also have to create each sub-array, while a two
dimensional array is just a single array that is created all at once.

In a jagged array each sub-array can have a different size (hence the
name), while in a two dimensional array the dimensions is the same for
the entire array.

--
Göran Andersson
_____
Cor Ligthert [MVP]
2/25/2007 12:00:00 AM
Very little correction on your for the rest very fine explanation.

[quoted text, click to view]

The first is a new syntax,

Cor

AddThis Social Bookmark Button