Declaration of global array

retkehing

Well-known member
Joined
Feb 5, 2006
Messages
153
Programming Experience
Beginner
I tried to put "Dim array(x, y) As Integer" under the public class form 1 and it stated error occured.

Dim array(3, 3) As Integer was fine whereas the system couldn't support when the x, y were replaced with 3, 3. How to define a global array with variables?
 
You can use x/y if they are variables that hold numbers, like this:
VB.NET:
Dim x As Integer = 1
Dim y As Integer = 2
Dim array(x, y) As Integer
Here is one article about arrays for starters: http://www.startvbdotnet.com/language/arrays.aspx
 
Just a quick tip

That is why I like the new .NET 2.0 because it does not allow this sort of thing to go unnoticed. If you do not initialize every variable and you call it later on it will display a warning just to make sure that a null reference does not occur. It is always a good programming habbit to initialize variables you dimension. Ex:

Dim str as string = ""
Dim int as integer = 0
Dim bool as boolean = True
Dim sw as system.io.streamwriter = Nothing
 
Dim x As Integer = 1
Dim y As Integer = 2
Dim array(x, y) As Integer

The above coding didn't work because x, y was still unknown to the system even i declared them before the array.
 
Ok, global yes? You get error "Constant expression is required.", no?
This will work:
VB.NET:
Const x As Integer = 1
Const y As Integer = 2
Dim array(x, y) As Integer
And this:
VB.NET:
Dim array(3, 4) As Integer
but there's not often need for any of these, especially not global, usually you just declare strong type arrays with zero elements for the dimensions, and later redimension as needed.
 
How to redim two dimensional arrays, e.g.
x=3
redim array(x, 2)
The error message states that only the rightmost value or 2 can be changed.
 
"If the Preserve keyword is specified, the new size for each dimension except for the rightmost one must be the same as the size of the existing array's."

So, what do you need this for? If it's for the points (x and y coordinates) in your other thread, then I have given you the examples to store the points. It doesn't matter if we are talking about a strong type array or an arraylist in that regard.

I advise you to find a good VB.Net beginners book or some tutorials on the web and start reading. This is entry level.
 
I know the rightmost is the only changeable dimension, but for example, if use for...loop to deal with my question in the GDI thread. How do i know the total no. of the ellipse the user is going to draw. I have totally no idea and it cannot be given a number by self assumption. If the user drew the ellipse more than the array could support then it wouldn't appear on the picture box. It is unlike the arraylist, as long as we perform add, then its size will be adjust accordingly. Hence, i was trying to change the size of the array, e.g array(no_of_ellipse, 1), 1 is for storing x(0), y(1) coordinates while no_of_ellipse would be increased one when each time the user draw the current ellipse.

Initially, no_of_ellipse=0 and no_of_ellipse+=1 after the first ellipse is drawn and so on in order to redim the proper size of array for storing next ellipse coordinate. So i hope there is a way out which the first dimension value could be changed to fit in this problem even i know it from the mdsn that it must be fix.
 
Back
Top