Class loses properties after method call

I am really stuck with this problem.
I have a class “BaseRect” and a Subclass “Field” with BaseRect as Super class.
The BaseRect has a String property “Identifier” which is read only and set only once in the Constructor.
After calling a method with the subclass as parameter the Identifier property gets lost.

Before method call:

After method call “AddField”:

Any ideas?

Does your subclass have a Constructor? That calls the Super constructor?

Also your naming convention is all over the place:

Fieldclass as Field

is just wrong on so many levels. I prefix all my classes with c, so the code would be for me

field as cField

Yes it has.
But the Constructor should only be called if I create a new instance of the class not?

Well, you must create Field somewhere … and the default for a string is the empty string …

As I explained above, you might confuse yourself somewhat with your naming convention …

The identifier is set in the Constructor of the BaseRect.
The class isn’t touched before and after the method call.
It is already created before and has an Identifier.

And my guess is that the constructor of the subclass overwrites it.

Even if the Constructor was called (why???) then it would call the Super.Constructor and in the Super.Constructor the Identifier would be set again.

Field
Public Sub Constructor()

Super.Constructor
End Sub

BaseRect
Public Sub Constructor()

mIdentifier = GetUniqueIdentifier(False)

// Calling the overridden superclass constructor.
Super.Constructor
End Sub

// Calling the overridden superclass constructor.
Super.Constructor

What is the super constructor doing?

The Super class of BaseRect is Rect.
https://docs.xojo.com/Rect

Look at my first screenshot.
The Identifier IS set.
After calling a method with my class the Identifier gets lost.

Error here - suddenly my constructor disappeared.

You have a property Identifier and mIdentifier.

In the constructor you set mIdentifier, but never identifier.

Identifier is only a computed property which returns mIdentifier.

Works here with a little example project. When Xojo gets into a state and starts to behave strangely then try a restart of the computer (or clear out the cache by hand) - in my experience a restart of Xojo isn’t enough.

Unfortunately not a solution.

As I said works here with a demo app. Make a demo on your computer and with your Xojo version and see if it works. If it does then step through the code of your project - does it go anywhere else? Search for all occurrences of Identifier. If you have Arbed then check for corruption of the project, or save as text, check, and reimport it into a new project.

And please change that naming convention. It is “variable as class”, and the variable is an INSTANCE, so naming a variable “somethingclass” is just confusing … if anything it should be “somethingInstance” but better just “something”.

Just a guess …try using

ByRef fieldclass as Field

Objects are passed byRef as default

1 Like

i think you cannot replace the actual object rather the byval value. With byref you actualy replace the original even if it’s an object. correct me if i’m wrong.

  1. ByRef vs. Reference Types – Xojo Programming Blog
  2. Some Follow-Up Regarding ByRef – Xojo Programming Blog

The moral of the story – ByRef is used when you want to alter the actual item. When its a value type like an integer this alters the value it holds. When its a reference type this would allow you to assign a new instance.

I thought that too until today:

By default all arrays and objects are reference types that are passed ByVal.
https://docs.xojo.com/ByRef

What, therefore, are you concluding from that?