Grouping Variables

TommyG
03-16-2008, 09:12 AM
Right basically, i want to asign lots of variables but instead of doing this.

Variable1 = 4
Variable2 = 4

etc.....

is there any way of saying

variable 1 to 12 = 4 ????

Chears Tommy g

AtmaWeapon
03-16-2008, 09:48 AM
Use arrays.

Dim variables(11) As Integer

For i As Integer = 0 to variables.Length - 1
variables(i) = 4
Next

The number in the declaration (Dim variables(11) As Integer) should always be one less than the number of elements you want; VB interprets it as the desired upper bound of the array, and the first element is 0. To access element n, you use indexer syntax and keep in mind that element 1 has index 0: variables(3) accesses the 2nd element.

Arrays can be resized dynamically via the ReDim statement. I could have written the code above like so:
Dim variables() As Integer

ReDim variables(11)
...You can use ReDim Preserve to make an array larger without losing any elements:Dim array(1) As Integer
array(0) = 4
array(1) = 4

ReDim Preserve array(4)
Console.WriteLine(array(0)) ' should be 4
Console.WriteLine(array.Length) ' should be 5Use this very sparingly; it has to copy all of the elements of the previous array and can be very slow. If you are calling ReDim frequently or in a loop, use List(Of T) or ArrayList instead; they are designed for the case where you aren't sure how big they should be.

Corbis
03-16-2008, 09:59 AM
You can also do this for Arrays:

Dim var() As Int16 = {1, 2, 3, 4, 5, 6}

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum