Connecting Tech Pros Worldwide Forums | Help | Site Map

questions about arrays and collections

Gilbert
Guest
 
Posts: n/a
#1: Feb 24 '07
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




=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
 
Posts: n/a
#2: Feb 24 '07

re: questions about arrays and collections


Gilbert wrote:
Quote:
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
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.
Quote:
2) can variable 'x' in the second case contain anything (string, integer
... together)?
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.
Quote:
3) what is the correct syntax?
dim x as arraylist
or
dim x as arraylist()
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.
Quote:
4) what to choose between:
dim x as array (or if no difference dim x() )
and
dim x as arraylist
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.
Quote:
5) what to choose between:
dim x as arraylist
and
dim x as list(of string)
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
_____
http://www.guffa.com
Gilbert
Guest
 
Posts: n/a
#3: Feb 24 '07

re: questions about arrays and collections


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.comschreef in bericht
news:%23rZqRSFWHHA.5092@TK2MSFTNGP03.phx.gbl...
Quote:
Gilbert wrote:
Quote:
>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
>
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.
>
Quote:
>2) can variable 'x' in the second case contain anything (string, integer
>... together)?
>
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.
>
Quote:
>3) what is the correct syntax?
>dim x as arraylist
>or
>dim x as arraylist()
>
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.
>
Quote:
>4) what to choose between:
>dim x as array (or if no difference dim x() )
>and
>dim x as arraylist
>
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.
>
Quote:
>5) what to choose between:
>dim x as arraylist
>and
>dim x as list(of string)
>
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
_____
http://www.guffa.com

Cor Ligthert [MVP]
Guest
 
Posts: n/a
#4: Feb 25 '07

re: questions about arrays and collections


Quote:
1)dim x as string() = dim x() as string = an array of string ?
>
Yes
Quote:
2) dim x as array() = an array of array? Is this the same as dim x( , )?
>
>
dim a as object = x(0)(0) gives in the first situation the first one.


=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
 
Posts: n/a
#5: Feb 25 '07

re: questions about arrays and collections


Gilbert wrote:
Quote:
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 ?
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.)
Quote:
2) dim x as array() = an array of array? Is this the same as dim x( , )?
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
_____
http://www.guffa.com
Cor Ligthert [MVP]
Guest
 
Posts: n/a
#6: Feb 25 '07

re: questions about arrays and collections


Very little correction on your for the rest very fine explanation.
Quote:
The first one is the new syntax,
The first is a new syntax,

Cor


Gilbert
Guest
 
Posts: n/a
#7: Feb 25 '07

re: questions about arrays and collections


Indeed. Thanks a lot.
And ... i swear: this is my last question:
Look at this: the first Dim works, the second doesn't: (error)
What do i create with the second Dim and how to use it?

Dim r As Array = Array.CreateInstance(GetType(Int32), 101)
r(0) = 3
r(100) = 5

Dim az As Array() = Array.CreateInstance(GetType(Int32), 101)
az(0) = 3
az(100) = 5



"Cor Ligthert [MVP]" <notmyfirstname@planet.nlschreef in bericht
news:uz$Vp0KWHHA.896@TK2MSFTNGP05.phx.gbl...
Quote:
Very little correction on your for the rest very fine explanation.
>
Quote:
>The first one is the new syntax,
>
The first is a new syntax,
>
Cor
>

=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
 
Posts: n/a
#8: Feb 25 '07

re: questions about arrays and collections


Cor Ligthert [MVP] wrote:
Quote:
Very little correction on your for the rest very fine explanation.
>
Quote:
>The first one is the new syntax,
>
The first is a new syntax,
>
Cor
>
>
I don't understand how that is a correction?

You removed "one" which makes it implied, but it's still there.

Then you changed "the new" to "a new", which doesn't change the meaning
of the sentence either.

So, what's the difference?

--
Göran Andersson
_____
http://www.guffa.com
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
 
Posts: n/a
#9: Feb 25 '07

re: questions about arrays and collections


Gilbert wrote:
Quote:
Indeed. Thanks a lot.
And ... i swear: this is my last question:
Look at this: the first Dim works, the second doesn't: (error)
What do i create with the second Dim and how to use it?
>
Dim r As Array = Array.CreateInstance(GetType(Int32), 101)
r(0) = 3
r(100) = 5
>
Dim az As Array() = Array.CreateInstance(GetType(Int32), 101)
az(0) = 3
az(100) = 5
>
The second one declares a reference to an array of arrays, then you
create an array of integer and try to assign it to the reference. The
reference is for an array and the object is an array, but the data type
of the arrays differ. Here's how you create an integer array:

Dim az as Integer() = New Integer(100)
or:
Dim az as Integer() = Array.CreateInstance(GetType(Int32), 101)

or, just for completeness, using the old syntax:

Dim az() as Integer = New Integer(100)
or:
Dim az() as Integer = Array.CreateInstance(GetType(Int32), 101)

--
Göran Andersson
_____
http://www.guffa.com
Gilbert
Guest
 
Posts: n/a
#10: Feb 25 '07

re: questions about arrays and collections


Ok thanks

"Göran Andersson" <guffa@guffa.comschreef in bericht
news:%23i$xZnNWHHA.5092@TK2MSFTNGP03.phx.gbl...
Quote:
Gilbert wrote:
Quote:
>Indeed. Thanks a lot.
>And ... i swear: this is my last question:
>Look at this: the first Dim works, the second doesn't: (error)
>What do i create with the second Dim and how to use it?
>>
>Dim r As Array = Array.CreateInstance(GetType(Int32), 101)
> r(0) = 3
> r(100) = 5
>>
>Dim az As Array() = Array.CreateInstance(GetType(Int32), 101)
> az(0) = 3
> az(100) = 5
>>
>
The second one declares a reference to an array of arrays, then you create
an array of integer and try to assign it to the reference. The reference
is for an array and the object is an array, but the data type of the
arrays differ. Here's how you create an integer array:
>
Dim az as Integer() = New Integer(100)
or:
Dim az as Integer() = Array.CreateInstance(GetType(Int32), 101)
>
or, just for completeness, using the old syntax:
>
Dim az() as Integer = New Integer(100)
or:
Dim az() as Integer = Array.CreateInstance(GetType(Int32), 101)
>
--
Göran Andersson
_____
http://www.guffa.com

Cor Ligthert [MVP]
Guest
 
Posts: n/a
#11: Feb 25 '07

re: questions about arrays and collections


Goran,

"The" new in the sentence as it is used, gives the idea that it replaces;
"a" new means that it is an addition, very simple in my opinion. Both are
completely valid, there is not any idea of replacing and there is not any
preference for one of those, so there is not a "the" new.

Cor

"Göran Andersson" <guffa@guffa.comschreef in bericht
news:%23AETpjNWHHA.1016@TK2MSFTNGP04.phx.gbl...
Quote:
Cor Ligthert [MVP] wrote:
Quote:
>Very little correction on your for the rest very fine explanation.
>>
Quote:
>>The first one is the new syntax,
>>
>The first is a new syntax,
>>
>Cor
>
I don't understand how that is a correction?
>
You removed "one" which makes it implied, but it's still there.
>
Then you changed "the new" to "a new", which doesn't change the meaning of
the sentence either.
>
So, what's the difference?
>
--
Göran Andersson
_____
http://www.guffa.com

=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
 
Posts: n/a
#12: Feb 25 '07

re: questions about arrays and collections


Cor Ligthert [MVP] wrote:
Quote:
Goran,
>
"The" new in the sentence as it is used, gives the idea that it replaces;
"a" new means that it is an addition, very simple in my opinion. Both are
completely valid, there is not any idea of replacing
What makes you think that just because something is new, it implies that
it replaces everything that was before?
Quote:
and there is not any
preference for one of those, so there is not a "the" new.
Oh, yes, there is a preference. I prefer the new syntax, but that is of
course only my own preference. :)
Quote:
>
Cor
>
"Göran Andersson" <guffa@guffa.comschreef in bericht
news:%23AETpjNWHHA.1016@TK2MSFTNGP04.phx.gbl...
Quote:
>Cor Ligthert [MVP] wrote:
Quote:
>>Very little correction on your for the rest very fine explanation.
>>>
>>>The first one is the new syntax,
>>The first is a new syntax,
>>>
>>Cor
>I don't understand how that is a correction?
>>
>You removed "one" which makes it implied, but it's still there.
>>
>Then you changed "the new" to "a new", which doesn't change the meaning of
>the sentence either.
>>
>So, what's the difference?
>>
>--
>Göran Andersson
>_____
>http://www.guffa.com
>
>

--
Göran Andersson
_____
http://www.guffa.com
Cor Ligthert [MVP]
Guest
 
Posts: n/a
#13: Feb 25 '07

re: questions about arrays and collections


>
Quote:
Oh, yes, there is a preference. I prefer the new syntax, but that is of
course only my own preference. :)
>
As I have read it, but I assume you knew that.

:-)

(By the way I have no preference at all, but there were some long
discussions in this newsgroup lately)

:-)

Cor


Closed Thread


Similar ASP.NET bytes