Newbie having trouble with New

  • Thread starter Thread starter Ricky W. Hunt
  • Start date Start date
R

Ricky W. Hunt

I've looked and for the life of me I can't find an explanation of "New". For
instance, what is the difference between:

Dim a As Integer

and

Dim a As New Integer

?

Is it just "optional" (meaningless)? The only BASIC I knew previously was,
well, BASIC and when you Dim-ed something you were creating it, thus it was
"new". I'm just not getting it. Any help would be appreciated.
 
Hopefully others will add more (the last VB I used was VB4), but I think it
creates an instance of an object. You can create a hash table by saying "Dim
hTable as HashTable", but you cannot use that table until you say "hTable =
New HashTable" or "Dim hTable as New HashTable". It's all part of that
object oriented thing which I'm still getting use to.
 
You dont need to explicitly use the New operator for integers or strings
although you can write it in, they are stored on the stack ( in most
cases ). However, when you instantiate a class as an Object, you must use
the operator New to create a new object on the heap.

Dim a As aClass '//Simply Defines a As a type aClass

Whereas

Dim a As New aClass
'Both declares a and assigns it a reference to the new object of the type
aClass.

HTH

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
* "Ricky W. Hunt said:
I've looked and for the life of me I can't find an explanation of "New". For
instance, what is the difference between:

Dim a As Integer

and

Dim a As New Integer

?

Is it just "optional" (meaningless)? The only BASIC I knew previously was,
well, BASIC and when you Dim-ed something you were creating it, thus it was
"new". I'm just not getting it.

'New' is not mandatory for value types. Integers, Longs and structures
are value types. You will need 'New' if you want to execute a custom
parameterized constructor on a structure, for example.
 
* "One Handed Man \( OHM - Terry Burns \) said:
You dont need to explicitly use the New operator for integers or strings
although you can write it in, they are stored on the stack ( in most

Depends on what you want to do with the string. In general, you need to
create a new instance of 'String' using 'New String(...)' or assign a
string constant.

\\\
Dim s As String
MsgBox(s.Length)
///
 
Dim s As String = "kjkK" works just fine






--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
One Handed Man ( OHM - Terry Burns ) said:
You dont need to explicitly use the New operator for integers or strings
although you can write it in, they are stored on the stack ( in most
cases ). However, when you instantiate a class as an Object, you must use
the operator New to create a new object on the heap.

So the "storage space" for the object is not set aside until you do the New
(on anything other ints and strings)? Why would you ever want to declare it
but not create it (ie. why would you ever NOT want to use New)? You can't
really do anything with the object until it's created can you?
 
Often you will not "New" an object that is being passed to you. Instead
you will assign that passed object to your variable. This is one reason
that you would create a variable and not 'New' it. There are others,
but they all end up being a variation on this theme.

Also, it is much more than just the "storage space". The way that .NET
works, if this is the first time, in YOUR application that the object
TYPE is called, then the code for the object itself MAY not have been
processed yet. This is due to a feature known as "Just-In-Time"
processing (also known as JITting.) What this about is if there is a
code path that is seldom if ever used, why bring it into memory at all?
When you do a "New" on the object, you are telling the system (or better
stated - the JITTER) that you are going to use the object, get it ready
for use.

As OHM and Herfried also stated, the primitive types are always
available, and do not require a 'New'. These are known as 'ValueTypes'.

HTH

David
 
Back
Top