When to use a class name by itself and when with new

If I create an employee class that has the properties name, title, salary, and I want an instance of it I say
var person as new employee and can then say txtName.text = person.

However I can also var person as employee and then later on use it.

var person as employee
person = listboxEmployees.RowTagAT(listboxEmployees.SelectedRowIndex)

I can use new as in
var person as new employee
person = listboxEmployees.RowTagAT(listboxEmployees.SelectedRowIndex) and this works

My question is how are the two lines treated differently by Xojo.

This is the same as:

Var person as new employee
person = Nil
person = listboxEmployees.RowTagAT(listboxEmployees.SelectedRowIndex)

The reference to the first instance is replaced.

Think of it this way. You stored an instance of employee in the listboxEmployees.RowTag. When you retrieved it using RowTagAt you are overwriting the value that employee held from the new instance (but never used).

Essentially it’s no different than this
var s as string
s = listboxNames.RowTagat(listboxNames.SelectedRowIndex)

Except that string which is a special object in Xojo that does not require you to use new.

In general, it’s a good idea to look at your use of New. If you create a new instance of a class, and then are replacing it with an instance from a RowTag, factory method, Dictionary, etc. then all you’re doing is causing more work for the app by forcing it to create the instance and then destroy the new instance. Not a big deal and relatively benign, but if you’re doing this several million times it adds up.

Thank you both for the explanation. I was somehow under the impression that you always had to use the new keyword.

Var drawer As Clothes // You create a "drawer" that will receive "clothes" objects, but right now it's empty (Nil).

Var drawer As New Clothes // You create a "drawer" with "clothes" inside, not empty.

Eh… I disagree.

// You have the idea for a drawer, but no drawer exists yet.
Var ShirtDrawer As Drawer

// You have the idea for a drawer and have created it.
Var ShirtDrawer As New Drawer

Though in reality, trying to represent these concepts as physical objects is hard because one could argue the Drawer class itself is the idea.


You know, I was trying to be didactic and not exact. Without new, no content, with new, with content, the instance, but some people don’t even know what a “instance” means. So a relaxed “vision” about part of the concept. And those words “quoted” there are for “this is not exactly this”… but…

1 Like

This is what I was going to chime in with. Drawer would be a class that holds Objects. You need an instance of one to do anything.

If I was going to make up analogies, I’d say that to have a nil Drawer you’re talking about the space where the drawer slides into whatever it goes in :slight_smile:

I know we’re trying to simplify, Rick, but I think the difference between “I have this space reserved for something” and “this object exists but is empty” is a very important one.

The focus here wasn’t to teach OOP, but the use of New keyword as requested by the OP, and what it causes, what differs when using it or not in a variable definition of an object type.

Clothes was a Class, that instantiated using the keyword new, creates an object. Drawer was just a variable (not a class) name to set the reader’s mind in a better mood to understand the, such variable “with content”, and “with no content” concepts, but for those uncomfortable with such “impure” didactic choice, consider it as “x” instead of “drawer”, so such name goes so much abstract that it does not clash directly with some mindsets and the idea of the object type (instance of the class).
Sorry for not being able to explain my intentions as expected by some, using 2 lines, and having to explain things much further later, all that without a real need.