Ok, so I'm a C# coder, and I have trouble even reading VB.NET, so please forgive what may turn out to be an incredibly dumb question. But I have some code which looks like this:
Function GetName(sourceObject as Object) as String
return sourceObject.Name
End Function
So, ignoring the fact that the syntax is probably wrong - how does VB.NET get the Name property from sourceObject? Inspecting it at run-time reveals that sourceObject is of a type that supports a property called Name with a getter, but what does VB.NET do in this case? Is there some extra code that is being generated by the compiler to somehow cast this automagically at run-time?
As you may be able to tell, I'm a little confused. Thanks in advance!
This is a case of late binding. So, if the sourceObject contains a property as Name it will return the value otherwise it will just throw an error that property not found.
Late binding in C# requires reflection so it throws compile time error, whereas in vb.net it can be done without reflection.
The Visual Basic compiler performs a process called binding when an object is assigned to an object variable. An object is early bound when it is assigned to a variable declared to be of a specific object type. Early bound objects allow the compiler to allocate memory and perform other optimizations before an application executes. By contrast, an object is late bound when it is assigned to a variable declared to be of type Object. Objects of this type can hold references to any object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With