Home > front end >  Are there any difference in these 2 ways to declare array?
Are there any difference in these 2 ways to declare array?

Time:11-11

    Dim a As String()
    Dim b() As String

One says that a is a type of string array

Another isn't very clear. Are they completely equivalent?

Why the 2 ways?

I supposed the first way is saying a is a String array instead of just string.

I am not so sure about the reasoning of Dim b() As String

I give it a try. The result is that b is a String() that points to nothing.

a is an integer whose value is -1

I am confused.

Why?

In functions we declare that a function accepts an array with something like this

Protected Overrides Async Function createNormalLimitOrderMultiple(orderList As BasicSimpleOrder()) As Task

Here, BasicSimpleOrder() simply tell that orderList is of type BasicSimpleOrder() and not BasicSimpleOrder.

So why doesn't Dim a As String() work

CodePudding user response:

In most situations you could treat them the same, however there are some differences.

Array size: You cannot specify the upper-bounds of an array if you put the parenthesis after the type, otherwise you will receive an error.

Dim foo As String(1) ' Compilation Error: Array bounds cannot appear in type specifiers.

Property Definitions: You cannot define an array if you put the parenthesis after the name of a property. The reason for this is because you can optionally include parenthesis after a property name definition and then optionally include a parameter list inside the parenthesis.

Public Class MyClass
    Public Property Property1() As String ' not an array
    Public Property Property2 As String() ' this is an array
End Class

Here is the documentation on property statements in Visual Basic .NET: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/property-statement

  • Related