Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Arrays In User Data Types
#3
To add to what justsomeguy said here is a demo for using a variable length string to hold an array of Long Values:
Code: (Select All)
Option _Explicit
' I modified this for LONG type only do one one for floats DOUBLE 2021-01-31  stored in handy
DefLng A-Z
ReDim i
Type t
    s As String
End Type
Dim v As t

For i = 10 To 0 Step -1
    setLong v.s, i, i ^ 2
Next i

For i = 0 To 10
    Print getLong&(v.s, i)
Next i

' just for kicks lets jump to 100
setLong v.s, 100, 100 ^ 2

' now check the index 90 to 100
For i = 90 To 100
    Print getLong&(v.s, i)
Next


Sub setLong (array$, index, value&) ' Luke's Method except option explicit requires mod, no variables needed for one type
    If Len(array$) < 4 * (index + 1) Then
        array$ = array$ + String$(4 * (index + 1) - Len(array$), Chr$(0))
    End If
    Mid$(array$, index * 4 + 1) = _MK$(Long, value&)
End Sub

Function getLong& (array$, index)
    getLong& = _CV(Long, Mid$(array$, index * 4 + 1, 4))
End Function

I also want to add the way to do variable length string arrays is to put a demilter like a comma if text that does not use commas between each item then I use:

You build the string anyway that you need just putting delimiter between each items and pull items out by item number ( this is also base 1 stuff ie the first item has index 1).
Code: (Select All)
'Description: Use Item$() Function to treat strings like arrays without having to use an array structure.
' This function does not throw a fit if you ask for an item number (index) it does not have, it just returns an empty string.
' In QB64, Functions can't return arrays through the function name, but they can return strings that the Item$() function can
' translate like an an array index.  nItem numbers are the same as Counting numbers positive integers starting at 1.
'  eg Item7$ = Item$(CommaDelimitedString$, 7, ",") 'get 7th Item in string
Function Item$ (s$, nItem As Long, delimiter$)
    Dim c As Long, d As Long, lastd As Long
    If Len(s$) = 0 Then Item$ = "": Exit Function
    lastd = 1: d = InStr(lastd, s$, delimiter$)
    While d > 0
        c = c + 1
        If c = nItem Then
            Item$ = Mid$(s$, lastd, d - lastd): Exit Function
        Else
            lastd = d + 1: d = InStr(lastd, s$, delimiter$)
        End If
    Wend
    c = c + 1
    If c <> nItem Then Item$ = "" Else Item$ = Mid$(s$, lastd, Len(s$))
End Function
b = b + ...
Reply


Messages In This Thread
Arrays In User Data Types - by Consolemu - 01-18-2024, 08:30 PM
RE: Arrays In User Data Types - by justsomeguy - 01-18-2024, 09:11 PM
RE: Arrays In User Data Types - by bplus - 01-18-2024, 09:49 PM



Users browsing this thread: 2 Guest(s)