Ho do you DIM a global array?

I need to create an array for a web project that is unique to each session so that I can store fairly static information during the session. I will have a timer event that checks to see if the array needs updating and do so if required. The problem is that I have DIM’d the array in the session open event but the array does not seem to be available elsewhere in the project.

Create an array property on the Session class.

Variables that are created with Dim are local to the method and go away when the method ends.

I have tried to do that but cant see where to select array in the “type”?

An array in itself is not a type. Append brackets to the type – like “integer (2)” for a 1-dimensional array with a size of two elements or just the brackets if you do not want to set its size.

Ok I have created a property under sessions with “string(500)” and elsewhere in my project I do the following:

Session.aryActiveClients(0) = “Hello”

But I then get the error “This is not an array but you are using it as one”

In effect I think I am trying to create a shared property that is a string array. In a DIM world it would be:

DIM aryActiveClients(-1, 1) as String

Search your project for aryActiveClients
Have you created a local variable as well as the global one?

You might be better off using a dictionary or a collection for this.

You don’t dim that in advance to 500
Just have a property on the Session class of aryClients as Collection

When you need to add one, do something like this:

if aryClients = nil then aryClients = new Collection end if aryClients.add "the new client"

Dictionary/Pairs allow you to add objects plus an associated piece of information in one go.


Sorry, my hints were a bit confusing.

Don’t append the brackets to the type but to the name. If you set up your property aryActiveClients (-1,1) as string , you’ll have a array unlimited in dimension 0 and limited to a depth of 1 in dimension 1 just like your DIM command would do.

Use the same syntax for a property:

Name: aryActiveClients(-1, -1)
Type: String

Thanks Jeff I will take a look at that as I have never used collections before.

I have almost got the property working but their appears to be an anomoly. If i set the name to aryActiveClients(-1, 1) and then try and add something to say (0,0) it gives me an out of bounds error but if I change the name to aryActiveClients(500, -1) then it works but of course the array is now limited to 500 entries which is not what I want to do. It would appear that the -1 in a property does not work the same as a DIM. Is this a bug or am I missing something?

If you check the debugger, you’ll find that your array has an undefined number of entries – hence the out of bounds error. The system will not add new elements on the fly.

If you use a one-dimensional array, you can always add new elements with the append method. This does not work with more-dimensional arrays, you’ll have to redim them first before you can use new elements.

That’s why Jeff proposed the pairs structure which allows you to append elements easily just like you can with one-dimensional arrays.

Ok I now realise that I should have listened to Jeff in the first place!!! I will go off and learn about collections. Thanks everyone for all your help and sorry if I am asking the stupid and obvious.

Dictionary is better than a Collection, and I can’t think of a case where you’d choose Collection over Dictionary.

FYI, Dictionary was the replacement for Collection. It offers indexed lookups so it’s faster.

Dictionary, Collection, Array … I am spollt for choice :wink: I remember basic before it even had multi dimensional array, now thats giving my age away!! Off to find out about Dictionary now, maybe someone else would like to suggest why you might use a Collection rather than a Dictionary, just in case anyone else reads this post in future.

I doubt you’ll find anyone to make that case, and I’d bet even some veterans will be surprised to learn that Collection still exists. They are very similar, but Dictionary is much, much faster as the number of values expands.

Looking at the docs I think you are right that it appears that Dictionary is the way to go.

And feel free to ask even more. I can find nothing stupid or obvious in your original question – I learned the basic BASIC idioms myself when you had to type line numbers and know it’s some way to expand this to object orientation. But it’s really easy to get a grasp on, and for the rest this forum exists :wink:

Thanks Ulrich, those were the days when we had to use line numbers and goto and even gosub if you were really posh!!

I am trying to use your suggestion of doing a redim before adding but its not working.

I have defined the aryActiveClients(-1, -1) as a session property, I then do a ReDim Session.aryActiveClients(-1, 1) in my first page and I then do a Session.aryActiveClients(0,0) = “Tom Jones” and Session.aryActiveClients(0,1) = “12345” but I am still getting Out of Bounds error.

Are you saying that each time I add a new item I would have to Redim increasing the first parameter by 1?

Yes, exactly. On the first client this would be redim Session.aryActiveClients(1,1) and on the second ReDim Session.aryActiveClients (2,1) and so on. The dim count starts with 1, whereas the elements are numbered from 0 on.

When Dim’ing an array, -1 means “empty”, so an array of (-1, anything) would be an empty array. The number indicates the largest value of that dimension (the upper bound, or “Ubound”), so the smallest multi-dimensional array is (0, 0), which is a 1X1 array.

Your alternative is to Dim the array as large as (or larger) than you’d ever need, then use separate variables to track what’s actually being used.