simon
New Member
Posts: 2
|
Post by simon on Sept 24, 2023 20:24:11 GMT 1
Hi
My name is Simon. I'm new to this forum. I'm 48 years old now and used GFA-BASIC alot during the late 80's and early 90's on AMIGA. I have been downloading GFA-BASIC 32 from Sjouke Hamstra again and again to play a little with it. But otherwise I acutally use PowerBasic. I think he is doing a great job in preserving this wonderful language and keeping it up to date.
I was looking for a solution to store UDT's in a Hash, which GB cannot do intrinsically. I tried to overcome this by storing a UDT into a String and then storing that one in a Hash. By now this approach seems to work. Anyone ever tried, also? Or any other ideas?
$Library "gfawinx" $Library "UpdateRT" UpdateRuntime ' Patches GfaWin23.Ocx
Global daten As Hash String
Type myType nm As String*20 dat As Int EndType
Global MYTYPE_SIZE As Int = SizeOf(myType)
Main: InitData PrintHash End
Procedure InitData Local d1 As myType, d2 As myType, d3 As myType
d1.nm = "Simon" : d1.dat = 100 d2.nm = "Hansi" : d2.dat = 200 d3.nm = "Sarah" : d3.dat = 300
daten["eins"] = StrPeek(V:d1, MYTYPE_SIZE) daten["zwei"] = StrPeek(V:d2, MYTYPE_SIZE) daten["drei"] = StrPeek(V:d3, MYTYPE_SIZE) EndProcedure
Procedure PrintHash Local s$ Local temp As myType
For Each s$ In daten[] MemMove V:temp, V:s$, MYTYPE_SIZE Trace temp.nm Trace temp.dat Next EndProcedure
|
|
|
Post by (X) on Sept 25, 2023 2:40:01 GMT 1
Hi Simon,
Your code seems to check out:
|
|
|
Post by dragonjim on Sept 25, 2023 3:27:20 GMT 1
Hi Simon,
Yes, I used a similar approach some years ago using memory blocks - essentially the same as using custom fixed length strings. The only difference is I stored the memory block address in the Hash instead of the String. Your approach is probably better as it is more stable.
Well done and welcome to the forum.
|
|
|
Post by dragonjim on Sept 25, 2023 4:08:58 GMT 1
...one possible improvement to your code would be to store the UDT in an array; this would allow you to use functions to store and retrieve data.
I'm not near a computer right now, but I will post some examples of what I mean when I am.
|
|
|
Post by (X) on Sept 25, 2023 14:31:07 GMT 1
Here's a clue: If you execute: Trace <some_UDT> GB32 automatically lists the contents of the UDT in the Debug Output window. This implies that GB32 can peer into a user data structure. Perhaps this can be leveraged to store all the properties/members of a UDT into a Hash automatically while preserving the property names.
$Library "gfawinx" $Library "UpdateRT" UpdateRuntime ' Patches GfaWin23.Ocx
Type myType nm As String*20 dat As Int EndType Dim mt As myType
Trace mt
Debug Output Window:
|
|
|
Post by (X) on Sept 25, 2023 14:56:10 GMT 1
This shows some built-in features related to UDTs.
Debug "- Did you perform a syntax check first? (Shift+F5)"
' Sample Type RECT - Int32 left, top, right, bottom EndType
Type test tstl(6 .. 7) As Long tsts As String*20 rc(2) As RECT EndType
Form frm1 Trace frm1
Debug.Show
Dim udt As Gfa_Type, i% For Each udt In Gfa_Types Try Debug "Type ";udt.Name(0) For i = 1 To udt.Count Debug " ";udt.Name(i); If udt.IsArray(i) Then Debug "(";udt.LBound(i);"..";udt.UBound(i);")"; EndIf Debug " As ";udt.TypeName(i);#9;" // Type: ";udt.Type(i) Next Debug "EndType" Catch Debug "Error in Print_type ";Err;Err$ EndCatch Next
Do Sleep Until frm1 Is Nothing
Debug:
|
|
simon
New Member
Posts: 2
|
Post by simon on Sept 25, 2023 18:06:01 GMT 1
Wow. Thats amazing! Not to mention that it'll take a moment for me to understand what you did. But definitly a direction to explore.
|
|