Fog Creek Software
Discussion Board

What do you use for dynamic data structures in VB?

I have a grid on a screen (employee scheduling here..but it don’t’ really matter!). Each row of the grid can have 21 days for display. This may change..but is not really the problem.

However, I need a dynamic structure for *each* day that allows me add MORE then one set information for each square (project info for a single slot).

I was heading for an array of collections (talking VB here). However, collections don’t allow a user defined type to be added.

Any suggestions as to what most use for this 2nd dimension? When the 2nd dimension is going to be of “N” values? I could use a simple array, and *ASSUME* a max number of entries for each of the 21 days. (5 or 6 elements deep...that would probably do). However, I don’t like assuming a fixed dimension here. (especially, since the number for each element is NOT need to “fix” or waste all those un-used elements)

I stared the following defs, but then realized that I can’t add a user defined type to a collection.

Suggestions welcome (even what you would use in another language is welcome!). In a string based language like Pick (mv-basic) I always had a dynamic structure, and this would not even require thinking to solve. I am still searching for a that easy to use dynamic structure when these types of problems arise. 

What do you folks use for “n” based, or non defined number of elements type structures? (gee...a linked list?).

I certainly could create a class object, as that *can* be added to collection. (that is probably what I will do). However, before I do this...any other suggestions?

Anyway..the code I started to write looks like:

Type gridspot
    ' data for each additianl job in a day spot
    lngOrderId        As Long    ' order id
    lngDetailId      As Long    ' order detail id
    strDescript      As String  ' order descripton
End Type

Type gridrow
    ' one row of our grid
    strName    As String      ' Employee name
    lngEmpId  As Long        ' Employee id
    dayinfo                          As New Collection
End Type

‘ code to add to a grid might look like:

Dim MyGrid(1 To RowsToDisplay) As gridrow

Dim OneDayofInfo    As gridspot

OneDayOfInfo.lngDetailId = rstE!lngDetailId
OneDayInfo.lngOrderId = rstE!lngDetailId
OneDayOfInfo.strDescript = strOrderDescipt

MyGrid(intdayPtr).dayinfo.Add DayInf

I like the above “add” to the collection, but DayInf cannot be added since it is a user defined type.

Ideas for dynamic structures?

Albert D. Kallal
Edmonton, Alberta Canada

Albert D. Kallal
Sunday, October 6, 2002

I had this problem a while ago and went with creating class & collection objects. 

I found it simpler to code, and read.

Jack lives over there ->
Sunday, October 6, 2002

What about a collection of collections?
Should be OK unless performance is a real issue.

Monday, October 7, 2002

Alternately, how about just a linked list structure?

Build an array of days with each day being the head element of a linked list. Then additional events can be created as instances of an event class and linked in to the appropriate day's linked list

Monday, October 7, 2002

Perhaps if you drive it the other way, from the data.

In Visual Fox, grids can contain grids, you can add a grid to a cell, that might be possible in other grid controls as well.  If not then a list box.

Your data structure no doubt has a 1-N table for events that day for that employee, point either the grid in the cell or the list box in the cell to that selection. 

Simon P. Lucy
Monday, October 7, 2002

Personally we've always used classes instead. One thing to watch out for with collections is that the key is turned into uppercase.
One of the side advantages of a class is that you can have a KEY property which removes the biggest disadvantage of a collection (You can't get the key back from an item number)
Oh another thought... large collections (greater than a 1000 objects) take a long time to clean up. If you're doing that you may want to add a seperate cleanup phase where you remove the objects from the collection (and can prompt the user) rather than setting the collection to nothing, 'cos this can cause your UI to stall.

Peter Ibbotson
Monday, October 7, 2002

First, ditch the user defined types.  VB shuns them (as you have noticed).

Second, use the 'Dictionary' object instead of collections.  It's very sweet.

Third, use classes.  Use classes for the items (if you need something more than simple values) and create your own classes for the collections (although, that's a bit tricky - the class wizard thingy can help you do that).

Wayne Venables
Monday, October 7, 2002

This may not be relevant, but here's my experience...

I had a VB6 project I was migrating to VB.NET. I had used variant arrays to store all kinds of things, mainly because of the convenience of the Array() function. I had nested arrays representing different types of entities, relying on code to decide what to do with different array representations.

In .NET, Array() assignments got upgraded to New Object() {...} declarations, which, if nothing else, highlighted the mess I had created.

Determined to find a better solution, I decided to represent them as classes instead. Wishing to keep it simple, I declared classes with public variables for all the values they could contain. I then set up multiple constructors for each class to handle the different array formats I had been using.

The result was not ideal, but it was certainly a lot more elegant then using Arrays. Using constructors, my structure could be translated line for line from arrays into classes, and the classes had only taken minutes to define.

I can think of ways I'd improve it further. I'd further specialise my classes, deriving them from a common base. I'd move much of the code which handles them into the classes themselves, turning them from dumb data stores into smart self-contained, error-checking objects.

But that is another day's work. For now, it's a huge improvement on what I had.


James Shields
Monday, October 7, 2002

Oops, forgot this isn't the .NET forum. My last post may not be relevant at all!


James Shields
Monday, October 7, 2002

actually, it's very relevant.  i was looking at this thread struggling with some massive VB6 programs i'm responsible for and i even began to compose a new topic to seek refactoring advice.  i was pretty much going to ask what i could do with the arrays i have.  your post shone (shined?) new light on my problem, even if i'm not currently migrating to .NET.


Monday, October 7, 2002

I was reading your thread and perhaps someone could help me...

I'm using direct3d8 to render stuff on the screen, one function really needs an arrays of this user defined type:

    X As Single
    Y As Single
    Z As Single
    color As Long
    Specular As Long
    tu As Single
    tv As Single
End Type

I've declared this Type in a Module i called Global.bas
and even if my type is now "officialy" public, vb6 wont let me declare an array of LITVERTEX as an Object Property!!! Why!!?!?

Marc Durocher
Monday, April 5, 2004

Your UDT needs to be publicy declared in a Publicly Createable class in order to use it as a a parameter/return value of an object method, or an object property.

Damien Steiger
Thursday, June 24, 2004

*  Recent Topics

*  Fog Creek Home