Dim and New

Sorry Waine: integers. Strings and text are not classes. You can’t create a derived class from them.
They are intrinsic types not classes.

Your call. I was only trying to explain how I see the world and perhaps share my viewpoint and how I reconcile these differences with the rest of you all.

To be clear I am an RF engineer who has retrained as a network engineer who has been a programmer since 1975. And I don’t recall Integers, Strings &Text being intrinsic at the assembler level (been a little while though). My assumption is therefor that higher up the scale, but below our Xojo level there are in fact classes to handle these “intrinsic” types that perhaps [quote=375234:@Wayne Golding]the compiler creates the instance[/quote].

This whole conversation seems to be about the confusion between primitive & class types and as I said I was hoping to share my head space.

Don’t worry Wayne.
Very often technical terms are used like common words in spoken language and this creates confusion.

[quote=375234:@Wayne Golding]So the way I see it:

Dim i As Integer = 10

is translated by the compiler into

Dim i(0) As Integer i(0) = New Integer i(0) = 10

This explains why you can’t have

Dim i As Integer

And

Dim i() As Integer

declared in the same method. It also explains why Dim is required - all properties are treated as arrays of 0 or more items.[/quote]

Sorry Wayne, but this is not true.

Trying to add a few words (my ISP is updating its cables and I have outages all over the day since the beginning of this month, probably at least until April):

I consider Dim as “reserve space for a datatype”, whereby a datatype in this case can also be an object.
In the case of intrinsic datatypes like Integers, Doubles and the like, the system simply reserves space according to the datatype’s needs: 8 Bytes for a Double, 4 or 8 Bytes for an Integer … The content of the reserved memory then determines its value. A String could be considered as a bigger chunk of memory, but still its Byte contents still determine its value. When an intrinsic datatype variable is changed, the memory contents are simply overwritten.

Class objects usually need more than only 4 or 8 Bytes. They can have a lot of properties attached to them, and the system needs to reserve space for the whole bunch (+ methods of course, although they do exist on a class level rather so the memory is not filled by duplicates of the same method). An object, like stated by Maurizio above, is in this state merely a ptr that points to address 0: Nil.

There are basically to ways of initializing such a reserved memory space: By assigning another instance of the same class to it like when receiving a value from a system function. Like

Dim f as folderitem // ptr space reserved, but pointing to Nil f = GetopenFolderItem("") // Returns an existing folderitem, or Nil if the user cancelled.

While f pointed to to address 0 after dimming it, it now points to the memory that represents the user chosen folderitem.

Or you can use New to initialize the space according to the object’s needs. Technically, the constructor method of the class is being called (or, if it has more than one constructor, one of them, depending on the input parameters you send with the New command). The additional memory necessary to hold all the object data is created (and often set to default or constructor values) and the Ptr behind the object now points to this space.

Dim d As Dictionary // space for a ptr reserved, pointing to Nil. d.value("test") = 1 // NilObjectException, because d was not initialized

Dim d As New Dictionary // object space reserved and the ptr set to point to this space d.value("test") = 1 // Now working.

The fact that the additional memory allocation and setting of default values takes some time makes it plausible why both exist:

Dim and code New[/code]: You would not want to waste CPU cycles when your object exists only to take a value from the system or some other method or API: The system would have to clear the additional space in a case like

Dim mb as new MemoryBlock (500) // Create a Ptr and reserve 500 Bytes of memory and make the ptr point to it mb = getHash(myPassword) // a method returning another memoryblock, so the 500 Bytes are declared as free again and mb points to the memoryblock space that getHash returned.

(Disclaimer: In reality things might be more complex. Take it as a simplified scheme)

[quote=375014:@Dave S]DIM is used to create an instance of a VARIABLE (which can include objects)
NEW is never used alone, but with DIM to indicate a new OBJECT
[/quote]
That’s not my experience. I (almost) always DIM an object and then NEW it to get an instance.

From the Introduction to Programming with Xojo guide, page 23:

Anyone still confused about dim and new should download the Introduction to Programming with Xojo guide found here: Xojo: Learn Xojo Programming

Dim (dimension), names, allocates, and sometimes initializes variables.
New instantiates objects (creates a new one).

I’ve read someone saying the New is not used alone without Dim. It can. And sometimes is, specially when you need something temporally and dispose the object just after, like:

MsgBox "Today is "+Date(New Date).LongDate

// Some other languages don’t need the “cast” part like: MsgBox "Today is "+(New Date).LongDate