VB.NET

Moderators: seancampbell
Number of threads: 4020
Number of posts: 10026

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
The NEW keyword Posted by mayitbedone on 12 Apr 2005 at 3:04 AM
Hi guys.

I'm back again. This time, I will like to know what's the major difference - in function - between using the New keyword in declaring a variable and not using it.

For example, we can declare variables as:
Dim varName as String
Dim varName as New String()

For the second declaration, I know an object is being instantiated but, I'm confused as to the difference in these methods of declaration since after the declaration, the variable name has got properties that can be called using the dot operator.

Please clarify.

Thanks.
Report
Re: The NEW keyword Posted by iwilld0it on 13 Apr 2005 at 10:52 AM
: Hi guys.
:
: I'm back again. This time, I will like to know what's the major difference - in function - between using the New keyword in declaring a variable and not using it.
:
: For example, we can declare variables as:
: Dim varName as String
: Dim varName as New String()
:
: For the second declaration, I know an object is being instantiated but, I'm confused as to the difference in these methods of declaration since after the declaration, the variable name has got properties that can be called using the dot operator.
:
: Please clarify.
:
: Thanks.
:

With ...

Dim varName as String


The variable is defined, but no value is established. The value is what you would call a NULL reference. In code you can test to see if an object variable contains a NULL reference like so ...

If varName Is Nothing Then
    ' WE HAVE A NULL REFERENCE
End If


Trying to call a property on a NULL reference results in an Exception being thrown ...

' EXCEPTION WILL BE THROWN
Dim length As Integer = varName.Length


---------------------------------------------------------------------

The latter statement ....

Dim varName as New String()


.... because of the "New" keyword, establishes an actual reference. This means that a memory pointer is internally returned that points to an area in memory large enough to store a String and a constructor method is executed to initialze the object. Since the reference is not NULL ...

If varName Is Nothing Then
     ' CODE IN HERE WILL NOT EXECUTE
End If


It is also legal to call properties on the object ...

' NO EXCEPTION THROWN -- The Property Call Works
If varName.Length > 7 Then
    ' CODE
End If


------------------------------------------------------------------

Now the String object is a unique choice to pick for demo purposes, because it is an intrinsic type of the language. In fact you can create a reference to a string like so ...

Dim varName as String = "Hello"


"Hello" is actually a String object, created by the compiler. Essentially, you are storing a memory pointer to "Hello".

To prove that "Hello" is in fact a string object, you can do this ...

Dim length As Integer = "Hello".Length


-----------------------------------------------------------------------

Feel free to ask any VB language specific question, for I am pretty competant with the underlying details of the language.
Report
Re: The NEW keyword Posted by mayitbedone on 14 Apr 2005 at 12:43 AM
Thanks pal.

Now it makes some sense to me.

Cheers.

: : Hi guys.
: :
: : I'm back again. This time, I will like to know what's the major difference - in function - between using the New keyword in declaring a variable and not using it.
: :
: : For example, we can declare variables as:
: : Dim varName as String
: : Dim varName as New String()
: :
: : For the second declaration, I know an object is being instantiated but, I'm confused as to the difference in these methods of declaration since after the declaration, the variable name has got properties that can be called using the dot operator.
: :
: : Please clarify.
: :
: : Thanks.
: :
:
: With ...
:
:
: Dim varName as String
: 

:
: The variable is defined, but no value is established. The value is what you would call a NULL reference. In code you can test to see if an object variable contains a NULL reference like so ...
:
:
: If varName Is Nothing Then
:     ' WE HAVE A NULL REFERENCE
: End If
: 

:
: Trying to call a property on a NULL reference results in an Exception being thrown ...
:
:
: ' EXCEPTION WILL BE THROWN
: Dim length As Integer = varName.Length
: 

:
: ---------------------------------------------------------------------
:
: The latter statement ....
:
:
: Dim varName as New String()
: 

:
: .... because of the "New" keyword, establishes an actual reference. This means that a memory pointer is internally returned that points to an area in memory large enough to store a String and a constructor method is executed to initialze the object. Since the reference is not NULL ...
:
:
: If varName Is Nothing Then
:      ' CODE IN HERE WILL NOT EXECUTE
: End If
: 

:
: It is also legal to call properties on the object ...
:
:
: ' NO EXCEPTION THROWN -- The Property Call Works
: If varName.Length > 7 Then
:     ' CODE
: End If
: 

:
: ------------------------------------------------------------------
:
: Now the String object is a unique choice to pick for demo purposes, because it is an intrinsic type of the language. In fact you can create a reference to a string like so ...
:
:
: Dim varName as String = "Hello"
: 

:
: "Hello" is actually a String object, created by the compiler. Essentially, you are storing a memory pointer to "Hello".
:
: To prove that "Hello" is in fact a string object, you can do this ...
:
:
: Dim length As Integer = "Hello".Length
: 

:
: -----------------------------------------------------------------------
:
: Feel free to ask any VB language specific question, for I am pretty competant with the underlying details of the language.
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.