What does the new keyword Mean

Hello, Could someone explain what the new keyword means. Brake it down into simple words ( i am still a begginer programmer so don’t know many technical terms ). The code i used looks something like this.

image_one = new picture(File.Width, File.Height)

I have looked at the Xojo reference page but still found it hard to understand what it meant. As mentioned above, it will be great if you could brake it down into simple steps and explain it to me .

Thank you

You know, there’s a number of great resources for beginners.

First, if you’re an absolute beginner to programming, there’s the textbook. It teaches you to program as well as use Xojo at the same time. Great two-in-one system.

There’s also a manual that comes with Xojo that teaches you the ins and outs of using the IDE, the language, and the features within. You can find that in the Documentation folder next to your installation of Xojo. There’s also an online reference http://documentation.xojo.com

Lastly, when you’re ready to learn from examples, there’s a folder full of example projects next to your Xojo install called “Example Projects” It helps when you don’t intend to reinvent the wheel. Do not copy and paste code from example projects, that won’t help you at all.

New creates a new instance of a class.

In your case, image_one is the new instance which is created from Picture, the class.

http://developer.xojo.com/new

If you don’t know what the terms instance and class mean, follow all of Tim’s advice from step 1. Good luck.

sounds the the term RTFM is beginning to apply…
There are tons of programming resources available including Google
And you really should do your own research first.
We can help you understand things to get to a working solution, but you should first prove that you have made an attempt

[quote=330621:@Gavin Smith]New creates a new instance of a class.

In your case, image_one is the new instance which is created from Picture, the class.

http://developer.xojo.com/new

If you don’t know what the terms instance and class mean, follow all of Tim’s advice from step 1. Good luck.[/quote]
Yes, if you just Dim image_one As Picture and then try to use it you’ll get an error since it doesn’t exist yet.

What about…

[code]Dim aPict As Picture
Dim f As FolderItem

aPict = Picture.Open(f)
if aPict <> Nil Then
Canvas1.Backdrop = aPict
End If[/code]

Code not checked…

Welcome to Xojo! Here are a few places that might help explain it better for you:

Hi Anamu,

with the Keyword Dim, Memory is reserved in the dimension necessary to hold the datatype, like

Dim i As Integer

which reserves 4 or 8 Bytes, depending on the architecture of your app (32 or 64 Bit).

Objects or classes are a different kind of beast:
While it is sufficient to reserve the space only if you receive an object from another method:

Dim d as Xojo.Core.Date = Xojo.Core.Date.now

where the class method now of the class Xojo.Core.Date returns the current date,
it is not enough if you want to create an object on your own, using one of its constructors:

Dim d as new Xojo.Core.Date (2017, 5, 12, 19, 30)

New initializes the object and calls the constructor (or the appropriate one for the parameters you pass).

When an application is loaded into memory additional memory is reserved by the operating system for that application: the memory needed to hold the application’s data. Very simplified it looks like this in memory:

.........[application].........[stack][heap].........

The stack and the heap is where the application store its data. Each application has its own stack and heap reserved by the operating system.

Stack: If the size needed for a variable is known at compile time, and the size of such variable does not exceed the size of the registers in the CPU, meaning 32-bit or 64-bit, the variable is stored on the stack. The memory address for such a variable can be calculated at compile time and is therefore hard-coded. Very fast. Used for scalars, pointers, and most arrays of scalars and pointers.

Heap: Class instances, the Objects in Xojo terms, go into the heap. The memory needed for an object usually exceeds the 32 or 64 bits size limit as the memory needed is the sum of all memory needed for all properties of a class. So the memory cannot be reserved at compile time, it is allocated at runtime. This is done by Xojo’s New operator. All these objects need two places of storage: a pointer on the stack holding the memory address of the object data stored in the heap. All this is slow compared to stack operations.

So when the compiler detects a line like this:

Dim i As Integer = 2

… the compiler knows he must reserve 4 (on 32-bit PCs) or 8 bytes (on 64-bit PCs). They are reserved on the stack. An example:

Dim i As Integer = 2 Dim i As Integer = 7 Dim i As Integer = 4

..........[Start of Stack|2|7|4|0|0|....|0|0|End of Stack]..........

Now when the compiler comes to a line like this:

Dim obj As MyClass = New MyClass() // or its shorter version: Dim obj As New MyClass()
… the compiler knows he must deal with an object. The compiler reserves 4 or 8 bytes for a pointer on the stack. This pointer will hold the memory address of the instance data in the heap, which uses memory allocated by New. An example:

Dim i As Integer = 2 Dim i As Integer = 7 Dim object1 As New MyClass() Dim object2 As New MyClass() Dim i As Integer = 4

..........[Start of Stack|2|7|pointer to object1 in heap|pointer to object2 in heap|4|0|....|0|0|End of Stack][Begin of Heap|....|object1|object2|....|0|0|End of Heap]..........

Dear Anamu Uenishi,

Welcome to this friendly and very helpfull community. I am sure you will find there the answers you seek when having a problem.

Please, ignore the very rude and unpolite statement “RTFM” made by Dave S because he was way out of line when writing that.

While other people already answered your question, I like to warn you about an error I made for years.

When you are using a FolderItem referencing a directory, file on your hard disk make sure you avoid using the keyword “new”. LIke :

dim canMyFile As FolderItem

then in another line under the dimension section :

canMyFile As FolderItem

It worked for years and I was thinking I got a completely fresh new folderitem. However, I was heading for trouble, because one day I was looking for days to solve a mystery error in the method which created my folderitems.

Just create a new folderitem wherever you want and which the scope you want but never use the keyword “new” on it because you can receive very unexpected results.

Chris

[quote=330618:@Anamu Uenishi]Hello, Could someone explain what the new keyword means. Brake it down into simple words ( i am still a begginer programmer so don’t know many technical terms ). The code i used looks something like this.

image_one = new picture(File.Width, File.Height)

I have looked at the Xojo reference page but still found it hard to understand what it meant. As mentioned above, it will be great if you could brake it down into simple steps and explain it to me .

Thank you[/quote]

Hi Anamu,

I have been in your situation before and it took me some time to grasp. I understand your situation. I remember what triggered my learning back then was an example of a car. Although It was another language that I learned this with, I found a similar article in the Xojo doc that explains similar to how I learned the NEW keyword.

http://developer.xojo.com/userguide/classes

If I may also use another example - that of a construction company building 10 houses on a street. He is given a blueprint of a house and he is to build 10 similar houses (based on the blueprint) on the street. Let just say the name of the blueprint is HouseBlueprint. So he will have to do something like the following to build the 10 houses (in a programming sense). Let’s just say:

Dim House1 as New HouseBlueprint
Dim House2 as New HouseBlueprint
Dim House3 as New HouseBlueprint
Dim House4 as New HouseBlueprint
Dim House5 as New HouseBlueprint
Dim House6 as New HouseBlueprint
Dim House7 as New HouseBlueprint
Dim House8 as New HouseBlueprint
Dim House9 as New HouseBlueprint
Dim House10 as New HouseBlueprint

So there the programmer has 10 houses - he created 10 instances of the same house from the HouseBlueprint. And all 10 houses will have the Methods, Properties, etc. that is in the HouseBlueprint.

Hope this can help you.