Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Array out of passing arrays...
#1
And it's way out!

So here is a method to use type variables, and still work with arrays, even though for some ****ing reason we can't assign arrays to a TYPE.

Code: (Select All)
Randomize Timer
Type foo
    x As Integer
    y As Integer
    one As Integer
    two As Integer
    three As Integer
    ind As Integer
    way As Integer
    object As String
End Type
Dim hp As foo
hp.object = "12345"
For hp.ind = 1 To 3
    Select Case hp.ind
        Case 1: hp.y = 1: hp.x = 35: hp.one = 1: hp.two = 2: hp.three = 3: hp.object = "first"
        Case 2: hp.y = 3: hp.x = 35: hp.one = 10: hp.two = 20: hp.three = 30: hp.object = "second"
        Case 3: hp.y = 5: hp.x = 35: hp.one = 100: hp.two = 200: hp.three = 300: hp.object = "third"
    End Select
    pete hp
Next
_Delay 1.5: hp.way = 1
Do
    hp.ind = Int(Rnd * 3) + 1
    If Int(Rnd * 10) = 1 Then hp.way = -hp.way
    pete hp
    _Limit 30
Loop

Sub pete (hp As foo)
    Static ArrayCounter$, oldind, i
    Static way(), y(), x(), one(), two(), three(), object$()
    i = hp.ind
    If InStr(ArrayCounter$, LTrim$(Str$(i)) + ",") = 0 Then
        ArrayCounter$ = ArrayCounter$ + LTrim$(Str$(i)) + ","
        ReDim _Preserve way(i), y(i), x(i), one(i), two(i), three(i), object$(i)
        hp.way = way(i): y(i) = hp.y: x(i) = hp.x: one(i) = hp.one: two(i) = hp.two: three(i) = hp.three: object$(i) = hp.object
    End If
    hp.y = y(i): hp.x = x(i): hp.one = one(i): hp.two = two(i): hp.three = three(i): hp.object$ = object$(i)
    way(i) = hp.way
    If way(i) < 0 Then
        If hp.x + way(i) > 0 Then hp.x = hp.x + way(i)
    Else
        If way(i) > 0 Then If hp.x + way(i) + Len(hp.object) <= _Width Then hp.x = hp.x + way(i)
    End If
    Color 8 - hp.ind
    Locate hp.y, 1: Print Space$(_Width);: Locate hp.y, hp.x: Print hp.object$;
    oldind = hp.ind: x(i) = hp.x
End Sub

So we don't have to declare our array in the main or pass it into the sub routine. Rather, the sub routine builds the arrays, and keeps them in memory. Now we just need to pass an index, as a type variable, so the sub routine knows which set of variables to pull from memory.

In this example, we have three words displayed on three text lines, and because of the sub routine memory, each can be moved independently. The conventional method would have required all of our variables built as arrays.

What do you guys think? And if you like it, please contribute to the "Buy Steve an aspirin" patreon account.

Pete
Reply


Messages In This Thread
Array out of passing arrays... - by Pete - 09-21-2025, 07:19 PM
RE: Array out of passing arrays... - by bplus - 09-22-2025, 03:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Arrays inside Types? Ikerkaz 7 170 Yesterday, 04:21 PM
Last Post: ahenry3068
  Arrays as UDTs Pete 26 1,121 02-06-2026, 04:31 AM
Last Post: bplus
  Preserving multi-dim arrays Pete 5 391 12-19-2025, 03:17 PM
Last Post: Dimster
  Huge array of variable length strings mdijkens 9 1,787 10-17-2024, 02:01 PM
Last Post: mdijkens
  An Array v's A Dictionary Dimster 10 1,828 10-08-2024, 06:59 PM
Last Post: TempodiBasic

Forum Jump:


Users browsing this thread: 1 Guest(s)