CodeBase - Arrays in UDT Workaround (Beginner-Intermediate)
Return to the CodeBase listing
Category: System Enhancements
Version: 1.0
Information
Uploaded: 17th Jul 2005 01:35
Modified: 17th Jul 2005 01:48
Author: The Wendigo
Summary
A way to handle a dynamic amount of data inside of a user defined type. This is a workaround which is very useful in all languages to an extent.
Full Description
First off, this may actually working in Classic as well as Pro but I haven't tested it. The method I present is actually very similar to how databases link information between tables. The idea is that you have an array of user defined types we will call Catagory. Next you have a second array of user defined types we will call SubCatagory. As you would guess, for our purposes SubCatagory should reside inside of Catagory, but Dark Basic does not support arrays in UDTs. So what we simply do is create an element in SubCatagory called Parent as an Integer. Parent is a handle to an index in Catagory. Then if I need all the SubCatagories related to Catagory(Index), I would loop through each SubCatagory and see which one's Parent equals Index. The source code should make it easier to follow.<br /> <br /> The source code contains two examples. The first example shows my way of taking care of arrays within UDT arrays. The second under "Equivelent Code" will not run under DarkBASIC but shows how another programming language might handle the same operations. <br /> <br /> There are, ofcourse, advantages and disadvantages for each method. The major disadvantage with the DarkBASIC (my) method is that you have to itterate through an entire pool of values pertaining to all Catagories every time you need to find subcatagories. This is a bit more time consuming as we are sifting through SubCatagories that don't belong to our Catagory, but there are ways to speed up the amount of time (buffering is a good example). The other problem is that we are dealing with a fixed pool of SubCatagories. This means that you will undoubtably over or under size your SubCatagory array leading to memory waist or buffer overruns. In the grand scheme of things, generally the former is not nearly as bad as the latter. The advantage is that the number of SubCatagories per Catagory is adjustable to the needs of that Catagory. Therefore, if you have a set of SubCatagories the size of 20 elements and one Catagory has 15 SubCatagories and the other has 5, then you won't have a memory waist and a buffer overrun like you would if you had the same two Catagories with SubCatagories inside each with 10 elements each.<br /> <br /> Whether you prefer one method over the other doesn't matter unfortunetly, because you are basically stuck with the method I've presented as DarkBASIC doesn't support arrays within UDTs. I think you will find that this method, in most situations, is not only painless, but relatively easy to manage.
Comments
No comments yet.