[OT] Creating a brand new Class (Insert --> Class)

Question for new and recent Xojo users (about Class).

Have you created one (or more) Class (Insert Class, not sub-classing a ListBox, TextField, TextArea, etc. to add a feature)?

If so, how did you get out of it?

Where did you find the necessary information?

1 Like

Hi Emile, are you asking how to “delete” the Class you created? Or where to find it in the IDE?

Not exactly sure what you mean, but I do this all the time. No Super defined. Usually they’re globally available for use throughout the app.

Var t As New MultipleThingClass
t.Name = "Thing"
t.Quantity = 2

Var Things() As MultipleThingClass

If you add a class, think of it like a window you cannot see.
You can add properties of the usual kinds
You can create a constructor method that initialises them
You can add methods to add on the data, or return their values.

But we have no idea what you mean by ‘get out of it’

Well, I won’t get into the power of subclassing, since you’ve said this isn’t about subclassing. :slight_smile:

I too am confused by “how did you get out of it?” I will take a stab, though. If what you mean is “how does one get rid of it when done?”, it’s simple. When you create an instance of the class in a method, it will be removed from memory when it goes out of scope.

Doh !

I typed my french text @ translate google and copy / paste the translation.

OK, I will rephrase it:

I searched in the LR / Documentation explanations on how to do a Class (Insert Menu -—> Class). I mean a step by step example that do something.
I searched on the internet, but there, they are talking to people who knows about Class.

I wasted hours yesterday to achieve a goal, using many different design until I realize it does not works, change the design, and so on… until at night, I found a simple solution (old school).

BTW: I fully understand the advantages of Class, what is unclear is how to build one that do something…

How to do what, though?

Here is a simple one: the ‘Savings Jar’ class.
It represents an empty jar where you save coins.

Insert a Class
Name it ‘Savings’
Give it a private property ‘Value’ as double
Give it a public method ‘Save’ which takes a double parameter
Give it a public method ‘Spend’ which take a double parameter, and returns a double
Maybe one called ‘Empty’ which returns a double
And one called TotalSaved which returns a double.

You should not be able to access the Value directly, which is why it is private

The Save method …

Save(v as double)
if v >0 then Value = Value + v   //cannot 'save' a negative

The Spend method

Spend(v as double) as double
dim ret as double
if v<= value then   //cant spend more than you have
   ret = v
   Value = value - v

  ret= value
  value = 0
end if
   return ret //confirm how much was removed

The Empty method

Empty() as double
dim ret as double
  ret= value
  value = 0
   return ret //confirm how much was removed

The TotalSaved method

TotalSaved() as double
dim ret as double
  ret= value
  value = 0
   return ret //confirm how much was removed

Now, you can have several jars …

dim myJar as new Savings
dim yourJar as new Savings

call myJar.save(25.3)
call YourJar.save(10)

dim GetSomeMoney as double
GetSomeMoney = MyJar.spend(30)   
//  we dont own 30, so GetSomeMoney will be 25.3
1 Like

I have used the same technique to create a class to replace an onscreen canvas
At one time I had a canvas on top of another canvas.
They both painted, but the result was horrible.

So I created a class which had the size properties of a canvas, but no actual graphics backing.
It still had a paint method, taking g as graphics

In the main canvas’ paint event, I drew the main canvas, then performed the drawing that the other canvas would have done, but at the location of the pseudo canvas, by calling the paint method and passing along ‘g’

ps: cant edit, but TotalSaved shouldnt have Value =0 in it,
I intended TotalSaved() to be a kind of ‘GetBalance’ function)

If you’re making an “abstract object” kind of class rather than a subclass, just leave the “Super” field blank in the Inspector for your new class. Then add methods, properties, etc to the class in the IDE as you would normally. Here is my class “Item”: