How do I declare and fill this array?

R

Ron

I want to decalre and fill an array with some numbers, 5, 7, 8, 9, 0

do I just do this:

Dim myarray as int = (5,7,8,9,0)

if not how would i do this?

thanks.
 
H

Herfried K. Wagner [MVP]

Ron said:
I want to decalre and fill an array with some numbers, 5, 7, 8, 9, 0

do I just do this:

Dim myarray as int = (5,7,8,9,0)

if not how would i do this?

\\\
Dim MyArray() As Integer = {5, 7, 8, 9, 0}
///
 
D

Doug Glancy

This makes me wonder, what's the difference between:

Dim MyArray() As Integer = {5, 7, 8, 9, 0}

and

Dim myarray As Integer() = {5,7,8,9,0}

thanks,

Doug
 
H

Herfried K. Wagner [MVP]

Doug Glancy said:
This makes me wonder, what's the difference between:

Dim MyArray() As Integer = {5, 7, 8, 9, 0}

and

Dim myarray As Integer() = {5,7,8,9,0}

There is no difference except in syntax.
 
R

Ray Cassick

One is correct and the other isn't :)

Dim MyArray() As Integer = {5, 7, 8, 9, 0} says exactly what you want. You
want an array of integer types.

Dim MyArray As Integer() = {5, 7, 8, 9, 0} really says that you want a
reference to an integer array.

While both will compile the second is a very ambiguous way to state what you
want.


Doug Glancy said:
This makes me wonder, what's the difference between:

Dim MyArray() As Integer = {5, 7, 8, 9, 0}

and

Dim myarray As Integer() = {5,7,8,9,0}

thanks,

Doug
 
C

Cor Ligthert [MVP]

Ray,

It is never good for a non native speaking English person to do a discussion
in that language.

But why is: "Create an object while giving it the name MyArray and use for
that an integer array" , not a more realistic way? (The myArray is just a
name, the array() tells the type to use and how many of those)

I have myself not any preference by the way, but telling that one is correct
and the other wrong is something I don't see using your explanation, even in
opposite of that.

Cor

Ray Cassick said:
One is correct and the other isn't :)

Dim MyArray() As Integer = {5, 7, 8, 9, 0} says exactly what you want. You
want an array of integer types.

Dim MyArray As Integer() = {5, 7, 8, 9, 0} really says that you want a
reference to an integer array.

While both will compile the second is a very ambiguous way to state what
you want.
 
A

Armin Zingler

Ray Cassick said:
One is correct and the other isn't :)

Dim MyArray() As Integer = {5, 7, 8, 9, 0} says exactly what you
want. You want an array of integer types.

Dim MyArray As Integer() = {5, 7, 8, 9, 0} really says that you want
a reference to an integer array.

While both will compile the second is a very ambiguous way to state
what you want.

The type of the variable is "integer array". The "()" (read: array) are part
of the type name. The variable type is put after the "As" keyword.
Consequently "As Integer()" (read: As Integer Array) makes more sense.

Armin
PS: There were already many discussions about ambiguous "()" (why are there
no "[]" for arrays like in C#?, why is it different if you specify an upper
bound?), so...
 
H

Herfried K. Wagner [MVP]

Armin Zingler said:
The type of the variable is "integer array". The "()" (read: array) are
part
of the type name. The variable type is put after the "As" keyword.
Consequently "As Integer()" (read: As Integer Array) makes more sense.

Well, I think that both declarations make sense, and I prefer the 'Dim
MyArray() As Integer' declaration for readability reasons (I think it's more
important that a variable contains an array than to read the type of its
items first).
 
T

Tom Leylan

Oh good a trivial argument... you get so few of these on USENET :) I'm
with Armin on this. If adding part of the variable's type definition onto
the variable name is a good idea then clearly syntax such as the following
would tell us that MyObject is a reference "before" having to see what it is
a reference to.

Dim MyObject* As Form

And as Armin intimates, if an IntegerArray keyword was defined nobody would
be questioning this at all.

Also Armin didn't actually "schrieb" what is stated in Herfried's reply.
The "One is correct and the other isn't :)" quote was written by "Ray
Cassick" <[email protected]> and while I note the grin in his
reply I will suggest that he is correct, he simply selected the wrong
example as the correct one :) What you have is reference to an integer
array so why not declare it as such?
 
C

Cor Ligthert [MVP]

Hi all,
What you have is reference to an integer array so why not declare it as
such?
Probably because that this fails
dim myarray as object(4)
and this not
dim myarray(4) as object

this fails as well, while it could be in my idea very proper code

dim myarray() as object() = {{1,3},{2,3}}

Cor
 
R

Ray Cassick

Boy, I suppose that I should have added the 'IMHO' to that response :)

The reason I state the ambiguity is... Try typing them into the IDE and then
hover the mouse over the variable. In BOTH cases the info tip states the
following:

Dim MyArray() As Integer

To me this leads to a specific amount of ambiguity, not something that is
good in code. While I have to admit that I have not looked at the underlying
IL for each case I would have to think that there would be some steps that
need to take place under the covers to 'fix' this.

I guess the bottom line is that *I* like the first one over the second.
 
H

Herfried K. Wagner [MVP]

Tom Leylan said:
Oh good a trivial argument... you get so few of these on USENET :) I'm
with Armin on this. If adding part of the variable's type definition onto
the variable name is a good idea then clearly syntax such as the following
would tell us that MyObject is a reference "before" having to see what it
is a reference to.

I don't think that the '()' is part of the variable name. Note that you can
specify the bounds there too. However, I believe that the "trivial
argument" is only my personal preference and that others may have different
preferences. That's why it's good that both syntaxes exist.
Also Armin didn't actually "schrieb" what is stated in Herfried's reply.
The "One is correct and the other isn't :)" quote was written by "Ray
Cassick" <[email protected]>

Sure, that's indicated by the ">>" at the beginning of the quoted line!

However, I do not fully agree with Ray Crassic's opinion although I share
his preference.
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
Probably because that this fails
dim myarray as object(4)


.... would have be

\\\
Dim MyArray As Object() = New Object(4) {}
///

.... or

\\\
Dim MyArray As New Object(4) {}
///

The curly brackets are necessary because otherwise the compiler cannot
distinguish between object instantiation (a call to the constructor) or
array dimensioning.

As somebody who doesn't want to type and read redundant information, I
prefer 'Dim MyArray(n) As Object'.
 
D

Doug Glancy

All,

Thanks for the education. I've decided I'll go with:

Dim() MyArray as Object <g>

Doug
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top