CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 1999
    Posts
    3

    How do I initialize a multi-dimensional array with pre-determined values?

    How the heck do I do this? Tried different methods, couldn't get anything to work.

    Example that works, is there a way to do this in C-style as follows:

    x[0][0] = 5;
    x[0][1] = 9;
    x[1][0] = 11;
    x[1][1] = 22;

    C-style
    x[2][2] = {{5,9}, {11,22}};

    TIA


  2. #2
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: How do I initialize a multi-dimensional array with pre-determined values?

    This is one limitation in VB. You cannot initialise while Declaring the var. as in C (int a[2][2] = {..})

    The trick is to define 'A' as variant.
    This works

    Dim ttt
    ttt = Array(Array(1, 2), Array(3, 4))
    ' ttt is actually not an array, but a variant
    ' behaving as an array: Using this style you
    ' can have a 2d array, whose 2nd dim varies!!
    ' And you use it like this:
    Debug.print ttt(1)(1) ' prints 4
    'Variable size 2dim array is defined like this
    Dim Ar2d() as integer
    Redim Ar2d(2,2)
    ' and assign each of elems seperately
    ' use
    Ar2d(1,1) = 4
    ' A regular 2D array is defined like this
    dim i2dAr(2,3) as integer ' a 2x3 array of ints, if option Base is 1 or 3x4 array if option Base is 0
    ' Use
    i2dAr(1,1) = 4



    Ravi


  3. #3
    Join Date
    Jun 1999
    Location
    Switzerland
    Posts
    58

    Re: How do I initialize a multi-dimensional array with pre-determined values?

    Hi,

    This is what I found* as an example:

    Option Explicit
    Dim SalesJan as Currency
    Dim TotalSales as Currency
    Dim Counter as Integer

    SalesJan = 15000
    SalesFeb = 22000
    SalesMar = 12500

    For Counter = 1 To 3
    TotalSales = TotalSales + SalesJan(Counter)
    Next Counter
    Debug.print TotalSales
    Stop
    End Sub

    * Ref: Spenic et all; Visual Basic 6 Interactive Course, Page 219

    I hope this helps

    Jan


Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured