Expected: expression in multidimensional dynamic array
I'm working in VBA under Word 2003 and I want a Module that initializes a scoring array for a set of documents. In my module1 I am trying to write:
Dim QScore(,,,,,,) As Double = New Double(,,,,,,) { _
{ 0 , 0.3 , 0 , 0 , 0 , 0 , 0 } _
... _
{ 0.1, 0 , 0 , 0 , 0.2 , 0 , 0 } _
}
Unfortunately I am new to VB and I keep getting an "Expected: expression" error and it highlights the first ',' in Dim QScore(,,,,,,)
I have tried a few variations I have found across the web to no avail. I would like to be able to access the array in this manner:
QScore(0,1) will be 0.3
QScore(1,0) will be 0.1
QScore(0,6) will be 0.0
Thanks in advance for any suggestions.
[782 byte] By [
csg] at [2007-11-11 10:12:58]

# 1 Re: Expected: expression in multidimensional dynamic array
to be dynamic u don't need to justify the dimentions :
dim qscore() as double
I think you try to make something like c++ or similar languages to use your array but in vb, the multidimension as I told u before (in dynamic case), and to specify it's dimensions and size u should use the keyword redim :
ReDim QScore(1,2,3,4,5) as double
Amahdy at 2007-11-11 17:22:55 >

# 2 Re: Expected: expression in multidimensional dynamic array
Okay, cool. I think my question is more how do I initialize the array from the get go, sort've like a grid/table? Redim works, as does Dim QScore(5,5,5,5,5,5) As Double, and I could just go through and mannually say:
QScore[0][0] = 0
QScore[0][1] = 0.3
...
QScore[6][4] = 0
But that seems overly tedious. I would like to declare the grid outright in an easily readable, modifiable manner. The error I run into if I say:
Dim QScore() As Double = { {0,0,0,0.3,0,0} }
I get an "Expected: End of Statement" with a highlighted '='.
Edit: Also, I realize now that I only need a Dim QScore(100,6), assuming I have 100 questions and 6 score types each.
csg at 2007-11-11 17:24:05 >

# 3 Re: Expected: expression in multidimensional dynamic array
the only way to initialize arrays in VB is to use the Array function (besides using the resources file):
dim a a variant
a = Array(1,4,2,5,6,10)
but: 1) I do not know if it is available in VBA and 2) I think it works only with mono dimensional arrays
Marco
mstraf at 2007-11-11 17:25:06 >

# 4 Re: Expected: expression in multidimensional dynamic array
first your error because in vb (high language) I don't think that there is a method to initialize variable or array .. (medium language yes of course) ..
second {...} aren't for vb at all
third [...] same as second :)
fourth redim is not like dim at all :
dim a() as double 'dynamic array
dim a(4) as double 'array size = 5
dim a(variable) as double 'compile error no variables to initialize arrays
dim a() as double
redim a(variable) as double 'ok and easier than (type= new type) in other languages ..
u may need to do that :
dim qscore(5,5,5,5)
qscore(0,1,0,0) = 12 .. and so on .
Amahdy at 2007-11-11 17:25:59 >

# 5 Re: Expected: expression in multidimensional dynamic array
the only way to initialize arrays in VB is to use the Array function (besides using the resources file):
dim a a variant
a = Array(1,4,2,5,6,10)
but: 1) I do not know if it is available in VBA and 2) I think it works only with mono dimensional arrays
Marco
Ah yes, this is the keyword I was looking for, thank you :) VBA (fortunately) does support it. After getting this keyword I figured out I could declare as such:
QScore = Array( _
Array(0, 0.3, 0, 0, 0, 0, 0), _
Array(0.1, 0.2, 0, 0, 0, 0, 0) _
... _
)
And as access I know that QScore(1)(1) = 0.2
Amahdy: Thanks for your suggestions also. You have me pegged pretty well, I am a C++ developer by trade :) As such, learning the conventions you've mentioned help greatly. You scared me for a second when you said I might have to write 126 lines of code just to fill a 21*6 array :eek:
csg at 2007-11-11 17:27:06 >

# 6 Re: Expected: expression in multidimensional dynamic array
We learn forever so thank you mistraf :)
btw I think it should be : qscore(1,1) = 0.2 //we don't use [..] AND [.][.] here :)
Amahdy at 2007-11-11 17:28:04 >
