Results 1 to 12 of 12

Thread: Trying to help...

  1. #1

    Thread Starter
    Lively Member TLord's Avatar
    Join Date
    Jun 2004
    Posts
    95

    Trying to help...

    I don't know if anyone reading this now will ned this.

    Anyway, there is a small thing I see in many code examples programmers offer in this forum and outside of it, this is about the intialization of objects using the New keyword.
    Why using this
    VB Code:
    1. Dim Obj as SomeType
    2. Obj = New SomeType(...)
    if you can use this?
    VB Code:
    1. Dim Obj As New SomeType(...)
    ? ? ? a modern programmer should always look for the compact and shortest way.
    Last edited by TLord; Jul 16th, 2004 at 04:46 PM.
    Do you think my life is easy?
    Do you think it's good to win?
    do you think it's nice to kill?
    Do you think learning is a must?
    Do you think computers are nothing?
    Do you think this post is stupid?
    Do ypu think we're really humen?

    DO YOU THINK IT'S GOOD TO THINK AT ALL? ? ? ! ! !

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    you may not always want to fire code that is in the new method when declaring the object

  3. #3
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    ? ? ? a modern programmer should always look for the compact and sortest way.
    That is inherently the wrong way to think. Why should it be compact and short? Better to be descritpive and informative. There have long been short cuts to make code compact, but better to be maintainable and easily understood by others.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  4. #4
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    If you are doing excally this

    VB Code:
    1. Dim Obj as SomeType
    2. 'without anycode inbetween here
    3. Obj = New SomeType(...)

    there is no reason not to do this:

    VB Code:
    1. Dim Obj As New SomeType(...)

    however, there was no option in vb6 to do it in one line and since most vb.net programmers come from vb6, you will see the first method by habbit.


    You should Never sacrfice quality for time/code length

    Although, when working on some projects, it may be acceptable to do this because of no other option.

    Even in temporary projects, or samples i create for ppl on the forum i still follow all the programming guidelines that i follow on a regular project. (force of habbit, but this one is good )
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  5. #5

    Thread Starter
    Lively Member TLord's Avatar
    Join Date
    Jun 2004
    Posts
    95
    Originally posted by SeanGrebey
    That is inherently the wrong way to think. Why should it be compact and short? Better to be descritpive and informative. There have long been short cuts to make code compact, but better to be maintainable and easily understood by others.
    So what's the job of Comments?!?!?

    Originally posted by <ABX
    however, there was no option in vb6 to do it in one line and since most vb.net programmers come from vb6, you will see the first method by habbit.
    Me too, I've came to VB.Net from VB6, byt I use the best out of the new features which can be a better replacement.

    And as a public answer, It's rarely needed to initialize the objects several lines after they're declared. For me I do this almost only when manually defining controls for a class that inherits System.Windows.Forms.Form class, ie coding a windows form manually without the help of WFD (Windows Forms Designer).

    YOU PEOPLE SHOULD REMEMBER THE TITLE OF THIS THREAD: "TRYING TO HELP...", I WANT ALSO TO HELP PEOPLE WHO CAME FROM VB6 AND DON'T KNOW ABOUT THIS KEYWORD
    Last edited by TLord; Jul 16th, 2004 at 04:53 PM.
    Do you think my life is easy?
    Do you think it's good to win?
    do you think it's nice to kill?
    Do you think learning is a must?
    Do you think computers are nothing?
    Do you think this post is stupid?
    Do ypu think we're really humen?

    DO YOU THINK IT'S GOOD TO THINK AT ALL? ? ? ! ! !

  6. #6
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    I seem to recall a recent post where it made a difference between early and late binding, but won't swear to it, or may have misunderstood.
    Ok, here's the post I was thinking of.
    Last edited by salvelinus; Jul 16th, 2004 at 05:17 PM.

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    The important point to bear in mind is not the difference between the two methods but the EFFECT of the difference.

    If you use early binding, you will not run into any problem later.

    If you use late binding you may run into a problem by carelessly trying to reference before the code runs trhough the binding. I know that if good practice is followed, you will not have a problem, but you could. This was discussed in a thread during the past week or so but I cannot remember it's title. Maybe Cyberhawke can.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by taxes
    Hi,

    The important point to bear in mind is not the difference between the two methods but the EFFECT of the difference.

    If you use early binding, you will not run into any problem later.

    If you use late binding you may run into a problem by carelessly trying to reference before the code runs trhough the binding. I know that if good practice is followed, you will not have a problem, but you could. This was discussed in a thread during the past week or so but I cannot remember it's title. Maybe Cyberhawke can.
    True , I always use early binding with the objs I create .

  10. #10
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    Originally posted by Pirate
    See the difference here :
    http://www.vbforums.com/showthread.p...ghlight=assign
    ooh! my post got a plug! lol

  11. #11
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I think you just need to make a judgement call. I normally do it all on one line, but in situations like the one below, I deviate because I only need one variable instead of two different ones.

    C#:
    Code:
    MyObject myObj;
    
    if(someValue > 0)
        myObj = new MyObject(someValue);
    else
        myObj = new MyObject();
    
    // Use myObj more down here.
    or VB.NET
    Code:
    Dim myObj As MyObject
    
    If someValue > 0 Then
       myObj = new MyObject(someValue)
    Else
       myObj = new MyObject()
    End If
    Notice how I didn't make a new instance of it right away, because it would have been a waste of processing because I might have needed to call a different constructor after the object was created. In the case above, this was the most efficient I could do it. (unless someone else knows otherwise).

    But, other than that case, I usually do this:
    MyObject myObj = new MyObject();
    or in VB.NET
    Dim myObj As New MyObject()

  12. #12
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by Grimfort
    where as in .Net the variable is only available below the declaration.
    Not quite correct.

    You can declare a form or public scope variable anywhere within the class after the Inherits statement provided it is outside the procedures or events and it will be recognised in an earlier positioned procedure or event.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

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