2

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!

Vimal Raj
  • 1,028
  • 13
  • 23
Matt Whitfield
  • 6,436
  • 3
  • 29
  • 44

4 Answers4

6

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.

Harsh
  • 3,683
  • 2
  • 25
  • 41
  • Ok thanks - but is there a reference somewhere that can describe to me exactly how it works? Because I need to convert this code to C# and I would like to replicate the functionality in a sane way (i.e. not using reflection if at all possible)... – Matt Whitfield Feb 15 '12 at 11:43
  • Have alook at this msdn link http://msdn.microsoft.com/en-us/library/0tcf61s1.aspx – Harsh Feb 15 '12 at 11:45
  • This will help you to do late binding in C# http://kristofmattei.be/2010/02/11/c-late-binding/ – Harsh Feb 15 '12 at 11:47
  • Have a look at http://stackoverflow.com/questions/8225328/late-binding-magic-under-vb-net-converted-to-c-sharp and http://www.vbforums.com/showthread.php?t=421966 – Harsh Feb 15 '12 at 11:49
  • Thanks. Informative - although I am pretty sure this makes baby Jesus cry a little bit every time it's used! :) – Matt Whitfield Feb 15 '12 at 11:50
  • 3
    +1. And if you don't like this "feature", and I must admit I hate it, use Option Strict On to disable it. Sadly Microsoft say [we can't have the dynamic keyword in VB](http://stackoverflow.com/questions/3305275/net-4-0-framework-dynamic-features-in-vb-with-option-strict-on/3608896) – MarkJ Feb 15 '12 at 12:23
3

If you are using c# 4.0 then you can try this.

return ((dynamic)SourceObject).Name;
Vimal Raj
  • 1,028
  • 13
  • 23
2

Because of inherent late binding, all VB variables of type Object are equivalent to c# objects which have been cast to dynamic -- or to put it another way, c# added dynamic in order to gain functional parity with VB.

So, the equivalent c# code would be ((dynamic) sourceObject).Name

I would look into doing this with Generics or an interface if possible, as thats a cleaner design.

jmoreno
  • 12,752
  • 4
  • 60
  • 91
  • Yeah I plan on re-doing it with strong typing, but before I refactor something I like to know how it *actually* operates... – Matt Whitfield Feb 17 '12 at 19:53
1

Just be aware that VB's Late Binding bypasses type checking with your compiler. If the Object being passed in doesn't have a Name property, an exception could be raised at run time.

Derek Tomes
  • 3,989
  • 3
  • 27
  • 41