I'm new How do i create a Class or array like this

How can I create some sort of class where I define the structure like this:

Fruit {

Color as string

Name as string

Quantity as int

}

And later in the code I can just add values like

Fruit.Add {

Color=“Yellow”

Name=“Banana”

Quantity=7

}

And then be able to for each or loop through or just pick something like txtColor.Text=Fruit[5].Color

You can’t define a Xojo class in code - instead, you add it as an object in the IDE via the Insert > Class menu command. Then, you add properties to the class, and later set values for those properties in code.

So if you have a Fruit class that has Color, Name, and Quantity as properties, you can create and populate an instance of that class thusly:

Var myApple as Fruit

myApple = new Fruit

myApple.Color=”Green”
myApple.Name=”MacIntosh”
myApple.Quantity=5

If you add a Constructor method to your Fruit class with appropriate parameters, you can also do this:

myApple = new Fruit(“Green”, “MacIntosh”, 5)

1 Like

Made the class and set up 3 properties. I have 3 text boxes set up and a button

Var myApple as Fruit
myApple = new Fruit
myApple.cClolor=txtColor.Text
myApple.Name=txtFruit.Text

Not sure if this is all i need to do? In c# you would need something to actually add the values. In any case, i can’t get the data back out using a for each or for/next

So, this is the basic class:

As you found, Color is a reserved word so you cannot add a new property with that name.

I prefixed all 3 fields with Fruit.

Adding a method called constructor allows you to set the 3 properties at the time you create a new instance of the class. (Indeed, if it is the ONLY constructor, you cannot create such an object unless you provide the 3 values - you could however add another Constructor method with no parameters if you wish )

Armed with this class, you can create items and add them to an array, then iterate through them, like this:


var ArrayOfFruit() as Fruit

ArrayOfFruit.Add new Fruit (“Apple”,“Red”,12)
ArrayOfFruit.Add new Fruit (“Pear”,“Green”,2)
ArrayOfFruit.Add new Fruit (“Orange”,“Orange”,76)
ArrayOfFruit.Add new Fruit (“Melon”,“Yellow”,1)

var msg as string

for h as integer = 0 to ArrayOfFruit.LastIndex

msg = “You have " + ArrayOfFruit(h).FruitQuantity.tostring
msg = msg +” " + ArrayOfFruit(h).FruitColor
msg = msg +" " + ArrayOfFruit(h).Fruitname

if ArrayOfFruit(h).FruitQuantity <> 1 then
msg = msg +“s”
end if

msgbox msg

next
2 Likes

May with “For Each” is nicer:

Var ArrayOfFruit() As Fruit

ArrayOfFruit.Add New Fruit ("Apple","Red",12)
ArrayOfFruit.Add New Fruit ("Pear","Green",2)
ArrayOfFruit.Add New Fruit ("Orange","Orange",76)
ArrayOfFruit.Add New Fruit ("Melon","Yellow",1)

Var msg As String

For Each theFruit as Fruit in ArrayOfFruit
  
  msg = "You have " + theFruit.FruitQuantity.tostring
  msg = msg +" " + theFruit.FruitColor
  msg = msg +" " + theFruit.Fruitname
  
  If theFruit.FruitQuantity <> 1 Then
    msg = msg +"s"
  End If
  
  MsgBox msg
  
next

Thank you, that works! Is there a way to make it Global so that i can grab the data from other windows? I have other vars that are in a module and its scope is set to Global and that works, but i can’t figure out how to make the Fruit class Global. I work in c#, Powershell and SQL all day so i’m trying to replicate a c# app i wrote in Xojo as a test.

Add a module.

Add ArrayOfFruits() as FRuit to the module as a Global property.

Remove Var ArrayOfFruit() As Fruit from the sample code

Oh cool beans, it works! Thanks so much for your help, i really learned a lot from this.

3 Likes

Fruit.Add

this could be a shared method and then in could memory the objects into a shared property.
means you can call a method direct with the class name.

1 Like