473,396 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

questions about arrays and collections

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

Feb 24 '07 #1
12 1392
Gilbert wrote:
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.
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.
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.
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.
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
Feb 24 '07 #2
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" <gu***@guffa.comschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
Gilbert wrote:
>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.
>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.
>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.
>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.
>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

Feb 24 '07 #3
1)dim x as string() = dim x() as string = an array of string ?
Yes
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.
Feb 25 '07 #4
Gilbert wrote:
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.)
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
Feb 25 '07 #5
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
Feb 25 '07 #6
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]" <no************@planet.nlschreef in bericht
news:uz*************@TK2MSFTNGP05.phx.gbl...
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

Feb 25 '07 #7
Cor Ligthert [MVP] wrote:
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
Feb 25 '07 #8
Gilbert wrote:
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
Feb 25 '07 #9
Ok thanks

"Göran Andersson" <gu***@guffa.comschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
Gilbert wrote:
>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

Feb 25 '07 #10
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" <gu***@guffa.comschreef in bericht
news:%2****************@TK2MSFTNGP04.phx.gbl...
Cor Ligthert [MVP] wrote:
>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

Feb 25 '07 #11
Cor Ligthert [MVP] wrote:
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?
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. :)
>
Cor

"Göran Andersson" <gu***@guffa.comschreef in bericht
news:%2****************@TK2MSFTNGP04.phx.gbl...
>Cor Ligthert [MVP] wrote:
>>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
Feb 25 '07 #12
>
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
Feb 25 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Spammay Blockay | last post by:
I've been tasked with doing technical interviews at my company, and I have generally ask a range of OO, Java, and "good programming technique" concepts. However, one of my favorite exercises I...
10
by: KN | last post by:
I know both are pretty much the same and it comes down to personal choice. But I have to make the choice for the team. Things so far that I am considering 1. XML documentation in C# -- thats...
1
by: jose luis fernandez diaz | last post by:
Hi, In the chapter 4 'Collections and Records' of the 'PL/SQL User's Guide and Reference Release 8.1.6' book there is the next paragrap: "For example, PL/SQL supports implicit (automatic)...
10
by: mike | last post by:
If I have 2 object arrays like: var txtobj = theform.getElementsByTagName("input"); var selobj = theform.getElementsByTagName("select"); and i want to iterate over them I'd like to combine...
3
by: Kurzweil | last post by:
I need to make a two dimensional array of objects. These objects are of type Influence. How do I declare such an array? Now I use: private object influences; influences = GetInfluences(); //...
18
by: Mike Bartels | last post by:
Hi Everyone! I have two Arrays A and B. Both arrays are byte arrays with 7 bytes each. The contents of array A and B are the same A = {1, 2, 3, 4, 5, 6, 7}; B = {1, 2, 3, 4, 5, 6, 7}; When...
1
by: Michael Fitzpatrick | last post by:
Transferring arrays from C DLL's to VB.Net I have a DLL written in C. This DLL reads a text file and creates a several very large arrays, 500,000 points and even larger. I would like the get the...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
12
by: Gilbert | last post by:
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
15
Samishii23
by: Samishii23 | last post by:
First, images... I have a project that, at this time and version I am working with, I have 648 main stay images, so to say, plus another 100 or so in side features. They are going to stored in a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.