array newbie question

M

marfi95

Newbie question:

What is the difference between the following two statements:

dim x() as string -- defines array of strings

dim x as string() - ????
 
H

Homer J Simpson

Newbie question:

What is the difference between the following two statements:

dim x() as string -- defines array of strings

dim x as string() - ????

Where did you see the second?
 
M

marfi95

I saw it in one of my teammate programs. I think it was put there by
mistake, but seems to work. I had never seen it declared like that and
didn't know it it was any different.
 
B

Brad Rogers

I think they are just the same but the notation is flexible, should be x()
as string in either case ?

Strings are unusual in that they are reference types that dont need the New
keyword, so they behave like value types. Not a standard array and they are
immutable.
 
H

Herfried K. Wagner [MVP]

What is the difference between the following two statements:

dim x() as string -- defines array of strings

dim x as string() - ????

There is no difference, both are array declarations of an array of strings.

However, note the difference if you specify an array bound:

\\\
Dim s(9) As String
///

vs.

\\\
Dim s As String() = New String(9) {}
///

I prefer the parentheses at the variable name.
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
And me at the used type name.

By instance

myTable as String()

Is what I would prefer

Mhm... This leads to an inconsistent look of the code if you are using
'ReDim' and 'ReDim Preserve' too...
 
A

Armin Zingler

Herfried K. Wagner said:
Mhm... This leads to an inconsistent look of the code if you are
using 'ReDim' and 'ReDim Preserve' too...


With ReDim you do not specify the type name.


Armin
 
J

Joergen Bech

Newbie question:

What is the difference between the following two statements:

dim x() as string -- defines array of strings

dim x as string() - ????

Here is an article about arrays in VB.Net:
http://www.knowdotnet.com/articles/arrays.html

It also mentions the different ways of declaring the arrays:

The old VB6 way is

Dim x() As Integer

and the new one

Dim x As Integer()

The old one could be read as "array of integer" and the
new one as "integer array".

In the end, the result is the same.

If you type

---snip---
Public Sub DimTest()
Dim x() As Integer
Dim y As Integer()

Dim xc As Integer = 5
Dim yc As Integer = 6

ReDim x(xc)
ReDim y(yc)

'----

Dim x2() As Integer
Dim y2 As Integer()

ReDim x2(5)
ReDim y2(6)

End Sub
---snip---

and decompile it with Reflector ("there he goes with Reflector
again"), you get:

---snip---
Public Shared Sub DimTest()
Dim num1 As Integer = 5
Dim num2 As Integer = 6
Dim numArray1 As Integer() = New Integer((num1 + 1) - 1) {}
Dim numArray3 As Integer() = New Integer((num2 + 1) - 1) {}

'----

Dim numArray2 As Integer() = New Integer(6 - 1) {}
Dim numArray4 As Integer() = New Integer(7 - 1) {}

End Sub
---snip---

(Notice the compiler optimization in the second batch where the
ReDim statements have disappeared).

But: Although you can write

Dim x3(5) As Integer

you cannot write

Dim y3 as Integer(5)

so I, for one, prefer the old way for consistency.

Regards,

Joergen Bech
 
C

Cor Ligthert [MVP]

Joergen,

It is the first time that I think of it, however I think that even this
would be syntactical much nicer if it was possible

X(5) as String()
5 items with the variable name X from which is the type a String Array

Just as thought,

(In fact it is in VB six items. However let us forget that crazy decision)

Cor
 
J

Joergen Bech

Joergen,

It is the first time that I think of it, however I think that even this
would be syntactical much nicer if it was possible

X(5) as String()
5 items with the variable name X from which is the type a String Array

Just as thought,

I would read that as an array of arrays :) Never mind.
(In fact it is in VB six items. However let us forget that crazy decision)

One of those things where the .Net framework at its core did
not support the old ways of doing things in VB. Easier to have
every array 0-based rather than having to take a base value into
account every time an array item is addressed, I suppose. I'm sure
that has been debated for the last 5 years.

To be more explicit in VS2002/VS2003 one would write

Dim x(6 - 1) As Integer

for an array of six items.

In VS2005, we can finally write

Dim x(0 To 5) As Integer

or

Dim x(0 To 6 - 1) As Integer

but the lower bound must always be 0 in those declarations.

Then, of course, there are those who just don't care and
write

Dim x(6) As Integer

when they want a "1 To 6" array. Never mind that they are
wasting index 0 and should be careful not to iterate through
the array using For Each ... etc.

I don't really miss non-0-based arrays that much, though the
lack of them has hurt a few times when upgrading old VB6
code.

Regards,

Joergen Bech
 
A

Armin Zingler

Herfried K. Wagner said:
... but you specify the size directly at the variable name!


Right, that's why I don't see an inconsistence. It's the size, not part of
the type name.


Armin
 

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