Results 1 to 9 of 9

Thread: late binding?

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283

    late binding?

    what is better?

    VB Code:
    1. dim a as myOBJ = new myOBJ()

    or


    VB Code:
    1. dim a as new myOBJ()

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I don't know of any particular difference. But what you are referring to is not late-binding as most people express it.

    I think what you were concerned about is perhaps an unnecessary boxing operation, or maybe even an implicit conversion that Option Strict wouldn't allow.

    However, Objects are just reference containers to other declared types, so you can do something as bizzare as this:

    VB Code:
    1. Dim x As New Object
    2.         Dim y As Integer = 5
    3.         x = y
    4.         MessageBox.Show(x.ToString)
    5.  
    6.  
    7.         Dim z As New ArrayList
    8.         x = z
    9.         MessageBox.Show(x.ToString)

  3. #3
    Lively Member TLord's Avatar
    Join Date
    Jun 2004
    Posts
    95
    The second statement is one of the shorthand techniques introduced in VB.NET, just like they introduced direct assignment and other stuff.

    Anyway you may use late initializing for objects when you want to use it several times:
    VB Code:
    1. Dim myObj as someType
    2. myObj = New someType()
    3. muObj.property1 = value1a
    4. muObj.property2 = value2a
    5. myObjCollectionA.Add(myObj)
    6. myObj = New someType()
    7. muObj.property1 = value1b
    8. muObj.property2 = value2b
    9. myObjCollectionB.Add(myObj)
    10. .
    11. .
    12. .
    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? ? ? ! ! !

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

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Latebinding is when an object is declared as a generic type instead of a strong type. Latebinding slows performance because all of the type checking must be done at runtime instead of during compile.

    VB Code:
    1. 'This is latebinding
    2. Dim obj As Object
    3. obj=New Form()
    4.  
    5. 'This is still latebinding is the same as the above
    6. Dim obj As Object=New Form()
    7.  
    8. 'This is NOT latebinding and is better code
    9. Dim obj As Form
    10. obj=New Form()
    11.  
    12. Dim obj As Form=New Form()
    13.  
    14. 'Again this is the same as the above
    15. Dim obj As New Form()
    The difference being that the first example, the latebinding example declares the object as the generic type Object but in fact stores a more specific type in it later. The fact that there is some runtime type checking makes this latebinding.

    The second example declares the variable as type Form because it will in fact hold an object of that type.

  6. #6

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    so is
    dim obj as new form()
    late binding or not? by saying it's same as above, it's still unclear...

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    No, it's not late binding. "Same as above" refers to the statement immediately above the last one.
    The idea is that if you dim something as a specific type, it's early binding. If you don't, it's late binding.

  8. #8
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Originally posted by marvinklein
    so is
    dim obj as new form()
    late binding or not? by saying it's same as above, it's still unclear...
    It's not late-binding, and neither is the two code snippets you supplied at the top of the thread.

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Originally posted by salvelinus
    No, it's not late binding. "Same as above" refers to the statement immediately above the last one.
    The idea is that if you dim something as a specific type, it's early binding. If you don't, it's late binding.
    Originally posted by nemaroller
    It's not late-binding, and neither is the two code snippets you supplied at the top of the thread.
    What they said.

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