Arrays in UDT Workaround (Beginner by The Wendigo17th Jul 2005 1:35
|
---|
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. 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. Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com Rem /* The Main Catagory */ Type tCatagory Var1 as Integer Var2 as Integer EndType Rem /* Our sub catagory (NOTE: if you need catagories of the same type, you could Rem just as well put a Parent in tCatagory */ Type tSubCatagory Var1 as Integer Var2 as Integer Rem /* This is our handle to the main catagory */ Parent as Integer EndType Dim Catagory(5) as tCatagory Dim SubCatagory(20) as tSubCatagory Rem /* Set each sub catagory to a random catagory */ For Index = 0 to 20 SubCatagory(Index).Parent = Rnd(5) SubCatagory(Index).Var1 = Index Next Index Rem /* Find all subcatagories of Catagory(2) */ For Index = 0 to 20 If SubCatagory(Index).Parent = 2 Then Print SubCatagory(Index).Var1 Next Index Rem /* ------------ Equivelent Code (Will not run) ------------ */ Type tSubCatagory Var1 Var2 EndType Type tCatagory Var1 Var2 SubCatagory(20) as tSubCatagory EndType Dim Catagory(5) as tCatagory For Index = 0 to 20 Print Catagory(2).SubCatagory(Index).Var1 Next Index |