Why can we skip New keyword for MemoryBlock Class?

Dim s As String = “abcde”
Dim mb As MemoryBlock
mb = s
’ mb.Size = 5 (1 byte for each character)

The above code works and stores a memory block for the created string.
As you can see we can create an object “mb” without the “New” keyword and this works

However when i tried to insert a class in my project , I had to instantiate objects with the New keyword unlike MemoryBlock Class otherwise it threw NilObjectException error*

Please help me understand this.

maybe the =s create a new memory block

what happen when you remove this =s ? the debugger show = nil over mb?

Ok so its an Exception ? And we should always create object using New keyword

There is one more thing , that when we are defining say
Dim mb As New MemoryBlock(4)

In this case we are using the constructor of MemoryBlock with 4 bytes of data and now we cannot create it without New keyword or else it throws error.

updated my post.

new objects should usually use the new keyword.

the docu wrote

You can initialize a MemoryBlock from a string without using the constructor and declaring its size.

Yes i checked that , so basically if we have a constructor in a Class we can either choose to use New or not depending on our will

But if we dont have a constructor in the Class we do need to use the New keyword always

I guess thats what i understood

if you not create a object you can assign a other object to the variable.
if you would use this

Dim s As String = “abcde”
Dim mb As New MemoryBlock
mb = s

the object get replaced by = s (somehow hidden code do something automatically)

1 Like

But String is not a Class its a datatype then s is not an object

the = do something, there is a method behind that convert the string into a memory block.

1 Like

Oh i thought that the string say “boy” gets converted to ASCII representation and then it would be stored in the memory areas which we can define as to how many bytes using MemoryBlock(3) where 3 means store memory for 3 bytes of data.

Thank you

I think its really useful when we are running low on memory , and we can define how much space a variable must hold

what is behind the scene can others explain better, for me its a black box.

1 Like

Why is such a word masculine in English and feminine in Spanish?

The important thing here is the description of MemoryBlock in the documentation.

Picture (also) worked without New (a long time ago), then one day the syntax changed and now you have to (not always) use New. The documentation shows how to do this.

At last, a MemoryBlock starts at MemoryBlock(0)…

Yes probably C/C++ language code. Those two languages are real power in their own ways :smiley:

strings have an encoding its not one byte each char generally.

First masculine in English and feminine in Spanish are different , one means male and another means female.

The question here asked was if we can Skip New in creation of MemoryBlock
Then when we try to instantiate our own Class objects we cannot skip New there , we must always have to use New.

Just with some predefined Classes like MemoryBlock and Picture as you mentioned we can Skip New…

It is not really skipped.

Dim or Var just creates a variable - it doesn’t create an object.

An object is created by New.

Alternatively you can assign an existing object to the variable (like mb = s which works because string can be implicitly converted to a memoryblock).

1 Like

Yes i understand your point.

But according to documentation of MemoryBlock

one can create
Dim mb As MemoryBlock
Dim mb2 As New MemoryBlock(4)

so we can skip New for MemoryBlock

But say i created a Class Called Animal then
Dim dog As New Animal //Works
Dim dog As Animal// NilObjectException Error

So as you see we can skip New for memoryblock objects but not for our own created objects

Works fine here. All I get is a warning about an unused variable.

Are you by any chance using dog twice?

Try

Dim dog As New Animal  //Works
Dim cat As Animal  // Works
1 Like

thank you i understood

This is invoking MemoryBlock.Operator_Convert(String). Operator_Convert is a special method name that overloads the conversion operator (=) and instantiates the object without calling its Constructor method.

3 Likes