Ho do you DIM a global array?

Thanks Ulrich that makes sense.

Kem I was thinking about doing that but was starting to think that if I set it to say 5000,1 and the web project had say 100 concurrent users would I start to see an issue with memory etc. The flip side is if I do a Redim as Ulrich suggests then am I going to get a performance hit as the server needs to expand the array every time I do the redim?

I don’t know that I’d worry about either. I just did a quick check of RAM usage before and after a Redim of a string array from (-1,-1) to (100, 100) and it barely registered (0.1 MB increase). A Redim at those sizes won’t be noticeable in terms of speed either unless you’re doing it in a loop, In that case, Redim as infrequently as you can.

Thanks Kem I think I will do the redim when the session first starts and once again if they add a new client to the list etc.

Thanks to everyone for help with this.

one thing came up to my mind: What do you need the array for?

Considering you talking about being trained in old-school BASIC, I wonder if this structure is due to the fact that former idioms could only use built-in classes.
If you would use your array for, let’s say, keeping up to date with customer IDs and their names, I guess the most convenient structure would be a custom class aryActiveClient which holds two properties: ID and Name.

This way you could deal with a one-dimensional array: aryActiveClients () as aryActiveClient and then use append and all the other array-features without having to think about (re)dimming your array.

I have saved a small project that allows you to play with this structure. In this case, I have set up both properties as strings, but you could easily add different type properties. Basically there’s a ID that is just the ubound of the array + 1 and a field for entering a name. After clicking on “add”, all the contents of the array are listed:

You will find the most important stuff in the action handler of the add button and the class declaration.

Ulrich, thank you so much for doing this for me, that really is above and beyond what I expected. I am going to download the project now and have a play. I think you are correct in that I am used to arrays and struggle big time with the concept of classes and properties. In fact the whole OOPs thing is causing some fun!! :wink:

You’re very welcome, Nathan. I hope it helps a bit.
I’m myself more or less a newbie to Xojo but thanks to this forum have taken quite a few hurdles which – being trained to old-school languages – confused me really seriously in the beginning. Once you get them, things are starting to become a lot of fun. So consider my posts as training control – if I can help others understand the concept, I probably won’t have it completely wrong myself :wink:

Talking thus, you may feel it inconvenient to address the array as a global property by using “app.” as first part of its name. If you want to avoid that, put aryActiveClients () in a module property instead. Then you can address it from anywhere by its name alone. I saved this under the same link and filename.

OOP is one of those things that makes little sense until it suddenly does. Once you have that “ah HA!” moment, things start to fall into place.

It helps to start thinking about your code as if they were physical objects.

I think it is a bit like having kids, one day you realise that you are doing an ok job raising them!!!

Ulrich, your example is helping the penny to drop but I am still a bit confused. I can see how you have created the class and properties, that is really clever, didnt know that you could do that. So lets say I build a class with 3 properties: ID, FirstName, LastName and I display the FirstName and LastName in a single column in the listbox. I need the ability to be able to sort the listbox by FirstName or LastName. I cant see how you would do that using the class as their does not appear to be a sort parameter?

Imagine you have this class

Class MyClass Property ID As Integer Property FirstName As String Property LastName As String End Class
… and somewhere you define an array of it:

arrMyClass() As MyClass

… then you can do the following to sort after LastName:

Dim arrLastName() As String For i As Integer = 0 To arrMyClass.Ubound arrLastName.Append(arrMyClass(i).LastName) Next arrLastName.SortWith(arrMyClass)
Now arrMyClass is sorted by LastName

You could store the instance of your class in a CellTag of the row. The instances of the class will sort along with the rest of the row.

Ok, so is the general rule that the class is just data and you use the presentation part (e.g. listbox) to deal with how you want the data sorted?

It depends on what you’re doing, but that’s certainly a valid approach. If your class contains more data than is displayed in your ListBox, you can certainly make use of it during sorts and other events.

Hmm, I think it is start to make sense, now I am going to hack Ulrich’s code to see what I can do with it. I am still waiting for the “ah-ha” moment :wink:

And keep in mind you do not have to sort the array in order to have it sorted in a listbox. You might als well purge the entire data into a listbox and then set its SortedColumn and ColumnSortDirection properties before calling the listbox.Sort method.
I believe the listbox.sort is quite fast, but I have not yet compared to to sorting the array instead.

Sometimes you will find the need to sort by invisible contents – like you print the entire name (Firstname Familyname) in a cell but want it sorted by the Familyname first. In this case you can extend the listbox by invisible columns (setting their width to 0%) and sort those.

Like I said, my memories for the time before the “a-ha”-Moment are quite fresh. So please keep on asking; I guess you’re not far from that moment.

Addendum: The easiest way to play around with the listbox.sort is by first setting the has heading property to true so you have column headers you can click on.
You then add an headerpressed event handler to listbox one in which you can set the sort order differently if you need to (like sorting invisible columns instead of clicked ones). Else the system will sort the columns in the usual way.
I have updated the download accordingly, but don’t lose your experimental code. I have not added a Firstname property.