Results 1 to 3 of 3

Thread: Dynamic arrays

  1. #1
    brainleak is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2016
    Posts
    11
    Reputation
    6

    Dynamic arrays

    Hello, this is my very first post here.

    I have been using VB6 for the last few years and I have recently decided to move on to .Net so I'm trying to learn the new features.

    In VB6 I would sometimes use the ReDim statement for 2 dimensional arrays like this:

    Code:
    Dim myVar As Double
    '...
    nCols = 8
    nRows = 5
    '...
    ReDim MyVar(nRows, nCols)
    '...
    but this doesn't work in vb.net so, what would be a way around?

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,801
    Reputation
    1705
    If you want that variable to refer to a 2D array of Double then you have to declare it as that type in the first place.
    Dim myVar As Double(,)
    '...
    nCols = 8
    nRows = 5
    '...
    ReDim MyVar(nRows, nCols)
    '...
    Also, keep in mind that arrays in .NET are zero-based, i.e. the first element is at index 0, and that explicitly sizing arrays is done by specifying the upper bound, i.e. the index of the last element. The upper bound is always 1 less than the length. That means that code above creates an array that is 9x6 rather than 8x5.

  3. #3
    brainleak is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2016
    Posts
    11
    Reputation
    6
    Actually I meant to write

    Dim myVar As Double()

    but then I forgot about the data type.

    So the difference with VB6 is the colon within the parentheses:

    Dim myVar As Double(,)

    Thank you!

    Quote Originally Posted by jmcilhinney View Post
    If you want that variable to refer to a 2D array of Double then you have to declare it as that type in the first place.
    Dim myVar As Double(,)
    '...
    nCols = 8
    nRows = 5
    '...
    ReDim MyVar(nRows, nCols)
    '...
    Also, keep in mind that arrays in .NET are zero-based, i.e. the first element is at index 0, and that explicitly sizing arrays is done by specifying the upper bound, i.e. the index of the last element. The upper bound is always 1 less than the length. That means that code above creates an array that is 9x6 rather than 8x5.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •