It's about class

I searched in the documentation and leaned how to add a Class and that is good.

I continued my quest, then downloaded the XojoExample.zio archive and found tons of things. I stopped my quest when I realized that I forget what I was searching :wink:

So, here Iam searching for a CLass fo Dummies or Class 101 (Québec !)


Something I can start learn from.

Ideas ? Advices ?

you never used a own class in xojo? only your own modules?

1 Like

Create a class. Add properties, methods, etc. Drag your class into a window or create an object in the code with Var xyz As New myClass and use it like any other class.

Or create a new class and enter an existing class as a super and extend this class with methods, constants, properties, etc. as you need them. Then you use this new class like any other.

You use the scope to control which methods, properties, etc. can be accessed via the class or which are only used internally within the class. As always


It’s not magic and once you get the hang of it, you won’t want to do without it. Your own classes are an essential part of object-oriented programming. :wink:

1 Like

Thank you Sascha.

I’ll be back.

No. And if you except the “Class are fantastic” here and there, there is nothing (I found nothing) that allow me to start using them.

This recalls me a guy who have 1 document word processor. He does not understand the concept of 1 use / 1 document;

So he type his letters in that document, print the pages he needs, save until a further need.

There are cases when you simply cannot do without. For example, the other day I needed to write an EditCopy handler for a ListBox that would copy its content as a JSON object. Only you cannot add a menu handler to a ListBox and if you add it to the window it won’t be invoked as the ListBox already handles the event and copies its content to the clipboard as text. The solution was to create a subclass of ListBox by dragging a ListBox to my project. To that subclass I could then add a menu handler for EditCopy. The nice thing was that once I had defined a custom ListBox I could insert these custom ListBoxes anywhere within my project (and I could also use it in other projects).

For complex data structures with components of different types, classes are often the best solution, rather than stuffing everything in a dictionary or JSON object. It’s more efficient when you specify exactly what the components are by creating properties of a specific type, as opposed to using variants for everything. You will soon notice that many methods only deal with the components of some class and could thus be encapsulated as (possibly private) methods of the class rather than as methods of a window or module. Also constants and localized strings that are only relevant to a class should be local to that class. The resulting code is much neater, cleaner, and also easier to maintain and to re-use in other projects.

1 Like

I have at least 30 of my own classes in my app.

https://documentation.xojo.com/getting_started/object-oriented_programming/index.html

Thanks Rick.

Iwill watch it (even if I have bad experience o video tutorials)


Example:
“To open the SIM Card, insert the provided too intise the appropriate hole.”
Sorry guy, no tools, and
 where is the appropriate hole ?
After sometimes I found an object I could use, and it takes times to find the correct pression to make it open !

Keep your eyes open Emile !

1 Like

I take the concept, but this will not help me to understand and use them.

@Emile_Schwarz

Keep in mind that just by using Xojo, you are using classes. Every control and just about every “thing” you use in a Xojo project is a class.

For instance, DesktopListBox is a class. The items that you place on a window are instances of a class. URLConnection is a class. So to say you have no experience is incorrect. You just haven’t created your own yet, but if you think about how these other things work, it might help you understand how your own might too.

5 Likes

I have several classes that I use in listboxes. For a listbox row, there is a RowTag you can add. But suppose I need several items of information for that row. Then I make a class with several properties, one property for each item of data that the row need to store (perhaps needed by the Paint event). Then I do this:

Var ptr as MyClass

ptr = new MyClass
ptr.var1 = 27
ptr.var2 = 43
ptr.var3 = False

myListbox.RowTagAt (row) = ptr

And now the rowtag has three data items in it. Magic!

3 Likes

That is a conrete example/use.

Thank you.

Seeing “ptr” used as a variable name gives me the same chills that seeing that Xojo don’t even emit a warning to this:

Var integer As Double

3 Likes

Indeed. But I couldn’t think of a better name in a hurry.

I’m used to use “obj” when explaining.

2 Likes

i think it depend on the project you are working on.

A class is quite similar to a module.

classes are useful to solve complex tasks.

or for separate data, example with a multiplayer game.

Players() As Player
Players.Add new Player("Player 1")
Players.Add new Player("Player 2")

but mostly to combine classes, to build a hierarchy.
the Players would be part of a Game class.
Player could have a list of cards and each is a Card class/object.

for each pl As Player in Players
pl.ProcessInput
next

You know. But there is nothing to know to add a Property, a Function or a Method to a module.
The access and access is extremely easy.

I will wait some times before been able to use create and a Class.

I started designing a need and stopped (two days ago):

A Class Album and menu Sub-Classes Songs:

Class Album

Properties:
Author/Goup
Title
Release Date
Company
Duration
Comments

and hold some songs (Class Song)

Class Song
Title
Number
inLP
inSP
inEP
Release date (if NOT inLP And inSP or/and inEP)
Duration
Version
Comments

In the Album Class, I can have a Method who count the number of songs, another that totalize the runnin time of the songs in the album, etc.

I seem to have a bloquage from the theory (above) and the implementation.
I also need some calm time to go further.

1 Like

Class Album would have a property Tracks() as Song. You could then add a method to Album like

Function RunLength() As Integer
   var length as integer
   For Each track In tracks
      length = length + track.Duration
   Next

   Return length
End Function
1 Like