Mr A
Mr A

Reputation: 1385

VB.net: Arrays and DataType difference

This is just a quick question:

Are there any difference between

dim b as byte()

to

dim b() as byte

does this also work for string, integers ...

Upvotes: 1

Views: 583

Answers (3)

Meta-Knight
Meta-Knight

Reputation: 17845

No, there is no difference in this case. And it's the same for any type of array (Integer, String, or any other class).

It would only make a difference if you'd want to specify the array length. This is valid syntax:

Dim b(5) As Byte

While this is NOT valid:

Dim b As Byte(5)

Upvotes: 2

Heinzi
Heinzi

Reputation: 172230

It's the same. However, note the following differences:

Dim b As Byte()           ' Declares a variable of type byte array, initialized to Nothing
Dim b() As Byte           ' Declares a variable of type byte array, initialized to Nothing 
Dim b As New Byte()       ' Creates a new, single byte with value 0
Dim b = New Byte()        ' Creates a new, single byte with value 0
Dim b = New Byte() {}     ' Creates a new byte array with zero elements
Dim b = New Byte() {1, 2} ' Creates a new byte array with two elements

This is a bit confusing, since in VB T() can mean (a) a constructor call of Type T with no parameters and (b) an array of T.

Upvotes: 2

Harry Steinhilber
Harry Steinhilber

Reputation: 5239

There is no difference. The later syntax is more for backwards compatibility. Before .Net, you would define an array variable of type byte. In .Net, however, it is a variable of type byte array. Both syntax will work for any type.

Upvotes: 0

Related Questions