locked
Difference Between Array Declarations RRS feed

  • Question

  • User2023521679 posted

    I am trying to learn more of the syntax of vb.net. I have seen arrays declared 2 different ways: with the parenthesis after the variable name and with the parenthesis after the data type name. What is the difference between the 2 methods or do they do the same thing?

     Dim int As Integer() = New Integer(4) {}

    And


     Dim int2(4) As Integer

    Wednesday, June 11, 2014 2:52 PM

Answers

  • User753101303 posted

    Hi,

    Yes no difference.

    Dim v as integer() is closer to C# that is the type is "integer()"

    Dim v() as integer is closer to how the variable is used in the code that is variable(index). If I remember you had only #1 in the first VB.NET beta but some were vocal about this notation change so #2 (which the way it is in "VB classic") declaration was reintroduced again in VB.NET.

    This is why you have 2 ways to proceed. Pick whatever style you prefer.

    BTW another difference with C# is that the number is how many elements are stored in the array while for VB this is the index of the last element... (which is still closer to how the variable is used). It's worth to know if going back and forth between VB and C# projects or even just trying to port some code.

    {} is the array initialiazer and the size is inferred that is Dim v() As Integer = {0, 1, 2, 3} is an array initialized with those 4 values.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 11, 2014 3:46 PM

All replies

  • User139467042 posted

    They do the same thing as you have them written: They both create a 4-cell integer array.

    Wednesday, June 11, 2014 3:32 PM
  • User753101303 posted

    Hi,

    Yes no difference.

    Dim v as integer() is closer to C# that is the type is "integer()"

    Dim v() as integer is closer to how the variable is used in the code that is variable(index). If I remember you had only #1 in the first VB.NET beta but some were vocal about this notation change so #2 (which the way it is in "VB classic") declaration was reintroduced again in VB.NET.

    This is why you have 2 ways to proceed. Pick whatever style you prefer.

    BTW another difference with C# is that the number is how many elements are stored in the array while for VB this is the index of the last element... (which is still closer to how the variable is used). It's worth to know if going back and forth between VB and C# projects or even just trying to port some code.

    {} is the array initialiazer and the size is inferred that is Dim v() As Integer = {0, 1, 2, 3} is an array initialized with those 4 values.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 11, 2014 3:46 PM
  • User753101303 posted

    Actually 5 cells (would be 4 cells in C#).

    Wednesday, June 11, 2014 3:48 PM