Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

The NEW keyword

mayitbedonemayitbedone Posts: 6Member
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.

Comments

  • iwilld0itiwilld0it Posts: 1,134Member
    : 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 ...

    [code]
    Dim varName as String
    [/code]

    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 ...

    [code]
    If varName Is Nothing Then
    ' WE HAVE A NULL REFERENCE
    End If
    [/code]

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

    [code]
    ' EXCEPTION WILL BE THROWN
    Dim length As Integer = varName.Length
    [/code]

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

    The latter statement ....

    [code]
    Dim varName as New String()
    [/code]

    .... 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 ...

    [code]
    If varName Is Nothing Then
    ' CODE IN HERE WILL NOT EXECUTE
    End If
    [/code]

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

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

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

    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 ...

    [code]
    Dim varName as String = "Hello"
    [/code]

    "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 ...

    [code]
    Dim length As Integer = "Hello".Length
    [/code]

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

    Feel free to ask any VB language specific question, for I am pretty competant with the underlying details of the language.
  • mayitbedonemayitbedone Posts: 6Member
    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 ...
    :
    : [code]
    : Dim varName as String
    : [/code]
    :
    : 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 ...
    :
    : [code]
    : If varName Is Nothing Then
    : ' WE HAVE A NULL REFERENCE
    : End If
    : [/code]
    :
    : Trying to call a property on a NULL reference results in an Exception being thrown ...
    :
    : [code]
    : ' EXCEPTION WILL BE THROWN
    : Dim length As Integer = varName.Length
    : [/code]
    :
    : ---------------------------------------------------------------------
    :
    : The latter statement ....
    :
    : [code]
    : Dim varName as New String()
    : [/code]
    :
    : .... 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 ...
    :
    : [code]
    : If varName Is Nothing Then
    : ' CODE IN HERE WILL NOT EXECUTE
    : End If
    : [/code]
    :
    : It is also legal to call properties on the object ...
    :
    : [code]
    : ' NO EXCEPTION THROWN -- The Property Call Works
    : If varName.Length > 7 Then
    : ' CODE
    : End If
    : [/code]
    :
    : ------------------------------------------------------------------
    :
    : 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 ...
    :
    : [code]
    : Dim varName as String = "Hello"
    : [/code]
    :
    : "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 ...
    :
    : [code]
    : Dim length As Integer = "Hello".Length
    : [/code]
    :
    : -----------------------------------------------------------------------
    :
    : Feel free to ask any VB language specific question, for I am pretty competant with the underlying details of the language.
    :

Sign In or Register to comment.