what is better?
VB Code:
dim a as myOBJ = new myOBJ()
or
VB Code:
dim a as new myOBJ()
Printable View
what is better?
VB Code:
dim a as myOBJ = new myOBJ()
or
VB Code:
dim a as new myOBJ()
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:
Dim x As New Object Dim y As Integer = 5 x = y MessageBox.Show(x.ToString) Dim z As New ArrayList x = z MessageBox.Show(x.ToString)
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:
Dim myObj as someType myObj = New someType() muObj.property1 = value1a muObj.property2 = value2a myObjCollectionA.Add(myObj) myObj = New someType() muObj.property1 = value1b muObj.property2 = value2b myObjCollectionB.Add(myObj) . . .
See if this helps .
http://www.vbforums.com/showthread.p...t=late+binding
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.
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.VB Code:
'This is latebinding Dim obj As Object obj=New Form() 'This is still latebinding is the same as the above Dim obj As Object=New Form() 'This is NOT latebinding and is better code Dim obj As Form obj=New Form() Dim obj As Form=New Form() 'Again this is the same as the above Dim obj As New Form()
The second example declares the variable as type Form because it will in fact hold an object of that type.
so is
dim obj as new form()
late binding or not? by saying it's same as above, it's still unclear...
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.
It's not late-binding, and neither is the two code snippets you supplied at the top of the thread.Quote:
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...
Quote:
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.
What they said.Quote:
Originally posted by nemaroller
It's not late-binding, and neither is the two code snippets you supplied at the top of the thread.