standard practice for simple variables is something like this
Dim x As Integer = 5
But is it possible to define an ARRAY where all elements are initialied to a given value
Dim x(3,3) As Integer = 5 // obviously does not work
standard practice for simple variables is something like this
Dim x As Integer = 5
But is it possible to define an ARRAY where all elements are initialied to a given value
Dim x(3,3) As Integer = 5 // obviously does not work
As the docs say:
So you can do:
Dim x(3) As Integer = Array(0, 1, 2)
but not:
Dim x(3, 3) As Integer = Array(00, 01, 02, 10, 11, 12, 20, 21, 22)
You could make a helper function but how to specify the bounds or a new row? Maybe like…
dim x(-1,-1) As Integer = Array2DInt(2,2, 0,1,2, 3,4,5, 6,7,8)
Function Array2DInt(XBound As Integer, YBound As Integer, ParamArray v() As Integer) As Integer(,)
dim r(-1, -1) As Integer
redim r(XBound, YBound)
for i As integer = 0 to v.Ubound
r(i mod (XBound+1), i \\ (XBound+1)) = v(i) //untested
next
return r
End Function
Or better, only take the XBound and figure out the YBound from length of v().
2d an array of pairs
more - array of tuples ?
Sorry… the question was… using a single default value.
without needing addtional functions, helpers or code
I have a 1000 element array, and the “default” is zero, but I want to init them to “5”
which I know can be done with For/Next etc… I was wonder if there was an alternative.
The answer is “no”
BTW. autocomplete kinda “infers” it can be done… but does give an error