Data structures and classes


I have a data file that I access in visual basic, which has this style of data type:
Type Sysrec
name as string
p as integer
r as integer
end type

How do I set up the class in xojo?

I’m working with a 10 x 3 array: name, p, r

I now know how to read the datafile into an array, but how would I call an item on a line of data with the data structure?


You can create a structure. See

Right click in the Contents bar on the left of the IDE window and choose Class from the popup menu that appears. By default this will create a class named Class1. You will probably want to use better name than Class1 so for your case, with Class1 highlighted in the Contents bar fill in the Name field in the input bar on the far right of the IDE window. As per your post, I would enter Sysrec in to the name field. Leave the Super field empty an don’t mess with the Interfaces popup since you are creating a simple data class.

Now right-click on the Sysrec entry in the Contents pane and move the cursor to the Add to “Sysrec” entry that should be at the top. This will cause a secondary menu to appear showing the things that can be added to Sysrec. You want to choose the Property option. This will place a property named “Untitled” under the Sysrec entry. Again go to the pane on the far right and give the name of the property and the data type of property. So, for the first one you would put in Name in the Name field and in the Type field enter String

Repeat this process two more times to enter the p and r properties making them integer types. You will now have a Sysrec data class with three properties. You may want to give more descriptive names than p and r for the integer properties

I have an understanding now about xojo structures, but still have a nagging question.
I have a 10x3 file with name,p,r as the data types.
I have the class property set up
as class sysrec
property: name as string
property: p as integer
property: r as integer

In my code, I have an array to read the data file
Right now, I type in the number 5 and get the simple value of count(5)

My question is this:
When I set up the structure, do I just type in name(5) to get that name?


I’m not sure what you’ve got set up now, but assuming you don’t need an actual structure and given your example of the sysrec class, you could create an array of that class:

 dim records() as sysrec

Not knowing how you read the data and not knowing if your sysrec class has a constructor, at the most basic level you could do something like this:

[code]dim temp as sysrec

temp = new sysrec = “Bob”
temp.p = 1
temp.r = 2

temp = new sysrec = “John”
temp.p = 3
temp.r = 4

Once you’ve got your array of records set up you can reference an item like:

dim n as string = records(1).name

Which would give you “John”

Right now, I have this set up, kind of a rough program, even though it is my first xojo program:
The file I use is a txt file, called data.txt

[code]Dim f As FolderItem
dim i, j,s as variant
dim myArray(105,3) as string
dim v() as string
dim txt_in as textinputstream

f = GetOpenFolderItem(“text/plain”) //defined as a FileType’(specialfolder.desktop.child(“somefile.txt”) // assuming its on desktop

for i=0 to min(105,v.ubound) /// in case there are more than 10 records

for j=1 to 3
    if i = 5 then
    Label1.Text=i + v(i)
    Label2.Text=nthfield(v(i),",",7)  'Number 7 gives the 7th position in the line
  end if
next j

next i[/code]

I have not set up the data structure yet, but in the old visual basic format it is something like this:
Type sysrec
name as string
p as integer
r as integer
end type

I intend on keeping the same data structure using the class method posted earlier, but
I am confused on the calling of the data. Right now, I type in i=5 and that gives me v(i) which gives me the line of row 5 and the Label2 text gives me the info in the 7th field of the line.

How would this change with the xojo structure class that I plan to use?

Please format code as code as this is unreadable as it is (use the the button with the <>).


If you have an array of Sysrec classes as you have discussed above, then to reference elements of that array, assuming it is named myArray you use code like this:

theName = myArray(5).name thePValue = myArray(n).p