Click to See Complete Forum and Search --> : How do I initialize a multi-dimensional array with pre-determined values?


ericjazz
June 3rd, 1999, 03:31 PM
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

Ravi Kiran
June 4th, 1999, 03:02 AM
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

Jan Businger
June 5th, 1999, 10:06 AM
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