differences in declaration and instantiation coding

Discussion in 'ASP .Net' started by Mark Kamoski, Sep 3, 2003.

  1. Mark Kamoski

    Mark Kamoski Guest

    Hi Everyone--

    Please help.

    What (really) is the difference between these 3 code snippets?.

    '1
    Dim objData1 As New DataSet()

    '2
    Dim objdata2 As DataSet = New DataSet()

    '3
    Dim objdata3 As DataSet
    objdata3 = New DataSet()

    Which is fastest?

    Is there ANY difference between them?

    Which do you prefer?

    Which do you suggest?

    What are the factors involved?

    Please advise.

    Thank you very much.

    --Mark
     
    Mark Kamoski, Sep 3, 2003
    #1
    1. Advertising

  2. Logically speaking, they are all exactly the same.
    I'm relatively sure the first two would compile to the exact same IL. The
    third one might also.
    You could always use ILDASM to be sure.
    I wouldn't expect any significant performance difference between them. You
    could put them in a loop 1000 times and time them to be sure if you care
    that much.
    Personally I prefer your first example, it's short, simple, descriptive and
    to the point.

    --
    I hope this helps,
    Steve C. Orr, MCSD
    http://Steve.Orr.net
    Hire top-notch developers at http://www.able-consulting.com



    "Mark Kamoski" <> wrote in message
    news:...
    >
    >
    > Hi Everyone--
    >
    > Please help.
    >
    > What (really) is the difference between these 3 code snippets?.
    >
    > '1
    > Dim objData1 As New DataSet()
    >
    > '2
    > Dim objdata2 As DataSet = New DataSet()
    >
    > '3
    > Dim objdata3 As DataSet
    > objdata3 = New DataSet()
    >
    > Which is fastest?
    >
    > Is there ANY difference between them?
    >
    > Which do you prefer?
    >
    > Which do you suggest?
    >
    > What are the factors involved?
    >
    > Please advise.
    >
    > Thank you very much.
    >
    > --Mark
    >
    >
    >
    >
     
    Steve C. Orr, MCSD, Sep 3, 2003
    #2
    1. Advertising

  3. Hello,

    "Mark Kamoski" <> schrieb:
    > What (really) is the difference between these 3 code
    > snippets?.
    >
    > '1
    > Dim objData1 As New DataSet()
    >
    > '2
    > Dim objdata2 As DataSet = New DataSet()
    >
    > '3
    > Dim objdata3 As DataSet
    > objdata3 = New DataSet()
    >
    > Which is fastest?
    >
    > Is there ANY difference between them?
    >
    > Which do you prefer?
    >
    > Which do you suggest?
    >
    > What are the factors involved?


    There is no difference.

    HTH,
    Herfried K. Wagner
    --
    MVP ยท VB Classic, VB.NET
    http://www.mvps.org/dotnet
     
    Herfried K. Wagner [MVP], Sep 3, 2003
    #3
  4. In article <>,
    says...
    >
    >
    > Hi Everyone--
    >
    > Please help.
    >
    > What (really) is the difference between these 3 code snippets?.
    >
    > '1
    > Dim objData1 As New DataSet()
    >
    > '2
    > Dim objdata2 As DataSet = New DataSet()
    >
    > '3
    > Dim objdata3 As DataSet
    > objdata3 = New DataSet()
    >
    > Which is fastest?
    >
    > Is there ANY difference between them?
    >
    > Which do you prefer?
    >
    > Which do you suggest?
    >
    > What are the factors involved?


    In VB.NET, there is no difference with any of those code snippets. They
    all get compiled into the exact same IL.

    --
    Patrick Steele
    Microsoft .NET MVP
    http://weblogs.asp.net/psteele
     
    Patrick Steele [MVP], Sep 3, 2003
    #4
  5. Mark,
    In addition to the others comments.

    > Which is fastest?

    I would expect the first two, in that the third one implicitly initializes
    the variable to Nothing, then you set it to a value.

    This may be more of an issue with Value Types over Reference types.

    However the time difference is going to be so extremely minute, that I don't
    actually worry about it. Also I would expect the JIT compiler to optimize
    the difference in Release builds.

    In other words they all would be equally fast.

    > Which do you prefer?

    I prefer the first one, unless I need the second one or third one.

    > Which do you suggest?

    I suggest using the first one, unless you need the second one or third one.

    > What are the factors involved?

    The first one is my first choice.

    The second one is needed when you are defining a variable of a base class,
    but you are initializing it with a value of a derived class or from a
    Factory Method someplace.

    Dim arg As String
    Dim f As BaseForm = BaseForm.CreateForm(arg)

    Where the MainForm.CreateForm method is a Shared Function (a Factory Method)
    that based on the arg, is able to create a handful of different forms all
    derived from BaseForm.

    Dim frm As Form = New MainForm()

    Where MainForm is derived from Form, but I really do not want a MainForm
    variable here. I don't use this very often, but once in a while, when I want
    to make sure the routine only operates on the base class members... (read I
    am attempting to limit coupling to the derived class).

    The third one is needed when your initialization needs to be done in a
    try/catch, while the variable needs to be available in the Finally block.

    Dim con as SqlConnection
    Try
    con = New SqlConnection
    Finally
    If Not con Is Nothing Then
    con.Close()
    End If
    End Try

    Hope this helps
    Jay

    "Mark Kamoski" <> wrote in message
    news:...
    >
    >
    > Hi Everyone--
    >
    > Please help.
    >
    > What (really) is the difference between these 3 code snippets?.
    >
    > '1
    > Dim objData1 As New DataSet()
    >
    > '2
    > Dim objdata2 As DataSet = New DataSet()
    >
    > '3
    > Dim objdata3 As DataSet
    > objdata3 = New DataSet()
    >
    > Which is fastest?
    >
    > Is there ANY difference between them?
    >
    > Which do you prefer?
    >
    > Which do you suggest?
    >
    > What are the factors involved?
    >
    > Please advise.
    >
    > Thank you very much.
    >
    > --Mark
    >
    >
    >
    >
     
    Jay B. Harlow [MVP - Outlook], Sep 3, 2003
    #5
  6. I would suspect that both 1 and 2 would run slightly faster, as they are
    shortcuts, and are a single instruction, whereas the third is 2
    instructions. However, the compiler might just be smart enough to compile
    all 3 to the same IL code.

    As to the factors involved, make sure you understand what each is doing. I
    have seen green developers use Number 1 or Number 2 when there was no need
    to instantiate the object (the object was instantiated later as a result of
    some other instruction), which is a waste of processor.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    http://www.takempis.com
    Big Things are made up of
    Lots of Little Things.

    "Mark Kamoski" <> wrote in message
    news:...
    >
    >
    > Hi Everyone--
    >
    > Please help.
    >
    > What (really) is the difference between these 3 code snippets?.
    >
    > '1
    > Dim objData1 As New DataSet()
    >
    > '2
    > Dim objdata2 As DataSet = New DataSet()
    >
    > '3
    > Dim objdata3 As DataSet
    > objdata3 = New DataSet()
    >
    > Which is fastest?
    >
    > Is there ANY difference between them?
    >
    > Which do you prefer?
    >
    > Which do you suggest?
    >
    > What are the factors involved?
    >
    > Please advise.
    >
    > Thank you very much.
    >
    > --Mark
    >
    >
    >
    >
     
    Kevin Spencer, Sep 3, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    1
    Views:
    488
    Jonathan Bromley
    Oct 17, 2003
  2. Replies:
    1
    Views:
    577
    Salt_Peter
    Dec 25, 2006
  3. Hicham Mouline
    Replies:
    1
    Views:
    444
    Noah Roberts
    Dec 3, 2008
  4. Home_Job_opportunity
    Replies:
    0
    Views:
    502
    Home_Job_opportunity
    Jan 8, 2009
  5. Home_Job_opportunity
    Replies:
    0
    Views:
    588
    Home_Job_opportunity
    Jan 14, 2009
Loading...

Share This Page