Results 1 to 3 of 3

Thread: [RESOLVED] [2005] New Question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    129

    Resolved [RESOLVED] [2005] New Question

    What is the difference between declaring a variable new
    like

    Code:
    Dim variable1 As New String
    and

    Code:
    Dim variable1 As String?
    Thanks!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] New Question

    You don't actually declare a variable as New. Declaring a variable is declaring a variable. Using the New keyword means that you are also creating an object and assigning it to the variable you just declared. This:
    vb.net Code:
    1. Dim obj As New Object
    is actually just shorthand for this:
    vb.net Code:
    1. Dim obj As Object = New Object
    You can see that the actual declaration, on the left of the "=" sign is exactly the same, but you are creating a new Object and using "=" to assign it to variable. You could expand that further to this:
    vb.net Code:
    1. Dim obj As Object
    2.  
    3. obj = New Object
    and the result is still the same. This time the declaration and the assignment are completely separated but the code is still functionally equivalent.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    129

    Re: [2005] New Question

    Thank you very much, now that is clearer to me! thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width