Can an object be destroyed and used again?

I’ve declared the following two classes in my project…

Protected Class Team
	Public Property Name as String
End Class

Protected Class Player
	Public Property Name as String
End Class

I’ve declared a variant in Window1 called, ‘MyVariant’…

Public Property MyVariant as Variant

For illustration purposes, I’ve put the following code into the Open event of the window…

Sub Open() Handles Open
  Var MyVariant As New Team
  // Do something with Team
  
  Var MyVariant As New Player
  // Do something with Player
End Sub

This produces the following error…

Window1.Open, line 4
Redefined identifier. This name is already defined by variable MyVariant
Var MyVariant As New Player

My question is, can an object be ‘destroyed’ and used again? I know that ReDim works on the size of an array and objects have Destructors that will be called when the object goes out of scope, but what’s the opposite of New?

Kinda.
Its the declaration that will be a problem.
ReUse should be OK

 Var MyVariant As Variant
MyVariant = new Team
  // Do something with Team
  MyVariant = new Player
  // Do something with Player

…but honestly, thats just a bug waiting to happen.
I inherited a VB project from a muppet some years back that used global variants called x and v
There was no way to know what they contained in other parts of the code.

3 Likes

Ah… thank you! I realize the error in my code… I shouldn’t have used Var… just assigned it.

Using Var declares a local variable of the same name as the window’s property, and you’re trying to do it twice, hence the error.

Just assign the values without Var.

Can an object be destroyed and used again?

Answer: NO

MyVariant is a REFERENCE to an object, not the object. You can use agaun the variable to make another reference.

That has nothing to do with “objects”, is a problem using the same variable name multiple times, maybe you can start here: https://documentation.xojo.com/getting_started/using_the_xojo_language/variables_and_constants.html

Just setting the reference to Nil

MyVariant = Nil

You didn’t ask this question, but I will gently challenge you on your use of the Variant type here. In an strictly typed object-oriented language like Xojo, it is almost always better to use the most restrictive type to hold your references - otherwise you lose all the advantages of type checking.

1 Like

Agreed! I was trying to think of a way to step through an importing process that needed to fetch data from an external system, one object at a time. The thought of having to create a property and related methods for requesting data, receiving data, handling errors, and providing status updates, within my import window… once for every object type… of which there are many… was not appealing.

In the end, I wasn’t able to think of a good way to abstract and reuse the code since each object was so unique. The idea that I could re-cast a variant to suite each object was tempting… until I tried to add a handler to it.

1 Like