Hi,
this might be a silly question. Sorry for that, i’m a biginner…
What is the Diff between
Dim XXX As String and
SIm XXX As New String
I have seen some codes with the word “NEW” some without.
Thanks.
Hi,
this might be a silly question. Sorry for that, i’m a biginner…
What is the Diff between
Dim XXX As String and
SIm XXX As New String
I have seen some codes with the word “NEW” some without.
Thanks.
Subject: Dim XXX As String Vs Dim XXX As New String
It all goes on the type of what you are defining. There are simple scalar variable types (such as strings, integers, floating point types etc) and these are not declared using New.
The other types of variables you will use are to hold objects. An object is where someone has defined a “class” (an example might be a class called “Cat”) and an object is an instance of the class (for example, my cat’s name is Minton, so Minton is an object of class Cat).
When you use the “New” keyword in the declaration, it not only declares a variable to hold an object, but also calls what is known as a “constructor” routine, that appears in the code for the class itself.
The constructor routine in a class performs any required initialisation to set up an object. Think of it as the routine that gives birth to the object. In LotusScript, the constructor routine is a subroutine that is always called New.
So when you see New in a declaration, it’s declaring that the variable is to hold an instance of the class named, and additionally runs the constructor routine so that a usable object is also returned. Without calling a constructor, the object will be equivalent to Nothing, and is not usable.
To distinguish between classes and objects, here’s a simple declaration:
Dim Minton as New Cat
I’ve made up the name Minton as that’s what I want to call my new object instance. The class is called Cat. After the statement runs I will have an actual instance of a Cat. If I just declared Dim Minton as Cat (no New keyword) and then tried to do something with Minton, you would get an “object variable not set” error, as Minton hasn’t been “constructed” yet with New. You could programmatically test if Minton has been born yet with “If Minton is Nothing then …”
That’s your vet’s tour of variable declarations, hope it helps!
ps I have assumed the target is LotusScript and not Java. Java complicates things by having both a simple scalar-type string datatype, as well as a “String” class. If you are new to object-oriented code, LotusScript will give you a much easier entry into things than Java.