When to use New?

When do we need to use the New?

I have a class with properties of type text and a dictionary. The text items don’t need new to be used, but Dictionary does… How do I know when to use new?

[quote=185946:@Hal Gumbert]When do we need to use the New?

I have a class with properties of type text and a dictionary. The text items don’t need new to be used, but Dictionary does… How do I know when to use new?[/quote]

Text is a string. Dictionary is an object. Objects need to be created.

That makes sense! Can objects be created in the property inspector? Can I set the default value to new dictionary, or do I always need to create it in code?

I think that would ruin some concepts of OOP. You can always declare an object without new as placeholder for a method returning such an object. If it would default to new and then get replaced by a method result, you’d have some memory leakage going on.

The way I usually do it is in Open ; Most of my projects have a Dico dictionary I use for all sorts of stuff.

Dico = New Dictionary

You always need to create it in code.

I suggest to read read/watch some OOP tutorials an understand Class / Object / Instance vs Types. This will surely help if you don’t have same OOP basics

If you have properties in your class which are objects and they need to be available when the class is created just instanciate them in the class constructor. Ie in your case you have a property myDict as dictionary then in the class constructor just have myDict = new dictionary. Voila your myDict property of your class is an object.

You can also make your class not require the New operator by defining an Operator_Convert method.

Doesn’t add it too much overhead? Just curious about this approach.

no more than calling any other method.

Hi,

I actually like using constructors to construct objects. It is part of the documentation aspect of the code.

For example, sometimes I construct an object using a name and other properties; and sometimes an ID. Alternatively, or in addition, one could also define computed properties that check if the dictionary was already created and if not create an empty one.