PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: UDT's in Arrays  (Read 340 times)

Petrus Vorster

  • FireFly3 Registered User
  • Senior Member
  • *
  • Posts: 392
UDT's in Arrays
« on: May 13, 2018, 03:24:49 PM »

I know everyone is now mostly busy with Freebasic, but if you could still help ona powerbasic question i will appreciate it.

I need to create an Array of 50 items. I have done this by loading the list of items from a file in a function in a module.
I first declared a UDt like:
Code: [Select]
'----------------------------------------
Type Tracker
    TPrefix As String * 2
    TText   As String * 16
    Used    As Integer
End Type
'-----------------------------------------
Type Virtualtracker
     Mytracker (1 To 50)  As Tracker

End Type

In the module i can easily add data to the array and i tested it, the information is loaded correctly:
Code: [Select]
im VArray As Virtualtracker
'-clear all
For t& = 1 To 50
 Varray.mytracker(t&).Tprefix = ""
 Varray.mytracker(t&).TText   = ""
 Varray.mytracker(t&).used    = 0
Next t&

Myfile& = FreeFile
Dim Xtracker As tracker

Open "Trackers.TRX" For Random As #myfile& Len = Len(Xtracker)
     For t& = 1 To 50
      Get #myfile&,t&, Xtracker
      If Xtracker.used = 1 Then GoSub FFFOUNDER
     Next t&

Close #myfile&
Exit Function
FFFounder:
    Varray.mytracker(t&).Tprefix = Xtracker.Tprefix
    Varray.mytracker(t&).TText   = Xtracker.Ttext
    Varray.mytracker(t&).used    = 1

Return
Exit Function

I want to other functions across the program to be able to read this list of 50 items from the UDT, but i can only access the data while i am in the module.
Any attempt to access the UDT from another function returns no data. I assume this has to be a issue one has to solve with a POINTER.
I need the speed of the in-memory array.
Any ideas would be welcome if you can figure out what i am trying to say....  :)
Logged

Paul Squires

  • Administrator
  • Master Member
  • *****
  • Posts: 8483
  • Windows 10
    • PlanetSquires Software
Re: UDT's in Arrays
« Reply #1 on: May 13, 2018, 07:33:07 PM »

Looks like you need to dim you Varray as GLOBAL

GLOBAL Varray() AS VirtualTracker

I guess that seems right? Hard to tell from my first look at your code.
Logged
Paul Squires
PlanetSquires Software
FireFly Visual Designer, WinFBE Editor

David Kenny

  • FireFly3 Registered User
  • Senior Member
  • *
  • Posts: 444
  • Windows 7
Re: UDT's in Arrays
« Reply #2 on: May 13, 2018, 08:56:49 PM »

I think Paul has it right, but wanted to add that the global declaration will need to be outside any sub or function.
Logged

Petrus Vorster

  • FireFly3 Registered User
  • Senior Member
  • *
  • Posts: 392
Re: UDT's in Arrays
« Reply #3 on: May 14, 2018, 02:13:03 AM »

Hi ALL

Nope, that doesnt help either.
I will try something else, 50 items isnt going to cause of much of a speed problem.

I will just load the process in the specific form and not bother with all this nested UDT's. That seem to be a problem here.

Thanks for the help guys.

_Peter
Logged

Eros Olmi

  • FireFly3 Registered User
  • Little Newbie FireFly
  • *
  • Posts: 17
    • www.thinbasic.com
Re: UDT's in Arrays
« Reply #4 on: May 16, 2018, 02:14:41 PM »

In your code it seems you are defining variable inside a function. That is a local, variable that is destroyed when function exit.

Suggested GLOBAL declaration should work and variable be seen across your modules as far as:
  • declared outside any function/sub
  • declared in a module that is loaded before the global variable is used

« Last Edit: May 16, 2018, 04:15:18 PM by Eros Olmi »
Logged

Petrus Vorster

  • FireFly3 Registered User
  • Senior Member
  • *
  • Posts: 392
Re: UDT's in Arrays
« Reply #5 on: May 16, 2018, 03:08:21 PM »

Hi all

Thanks a million. Will Go give it a try and let you know if i got it working.

-Pete
Logged