views:

467

answers:

3

Is there an equivalent for the C# 4 'dynamic' keyword when using 'type safe VB.Net', i.e. with Option Strict On?

+1  A: 

The equivalent is Object in VB.NET but with Option Strict Off. With Option Strict On there's no equivalent. Put another way the dynamic keyword brings Option Strict Off equivalent functionality to C#.

Darin Dimitrov
That's what I was afraid off, thanks for confirming this.
jeroenh
+1  A: 

You can turn Option Infer On and Option Strict Off and still have something very close.

Joel Coehoorn
+3  A: 

Hmya, VB.NET always had the "dynamic" feature built in. This syntax was supported forever:

 Dim obj = new SomeComClass()
 obj.DoSomething()

The dynamic keyword was C#'s way of catching up to what VB.NET could do for a long time. Nevertheless, VB.NET version 10 did gain some additional capabilities, it is using the DLR as well. Which allows you to interop with IronPython and IronRuby. C# copied many other VB.NET features. Like optional parameters and support for properties that take arguments.

The syntax is exactly the same, use the Dim keyword without As. You will however have to use Option Strict Off, Option Infer On can soften that blow a bit. It does show that C# adding the dynamic keyword was a pretty good move.

Hans Passant
@Hans Passant: I know that, however C# dynamic is not quite the same as VB's 'late binding'. In C#, with the dynamic keyword I can be very explicit about the dynamic parts of my program. To me it's like telling the compiler: "hey, I know what I'm doing for this specific part of my program". With VB.Net I have to turn off Option Strict for my whole project, which can lead to subtle bugs creeping in because of it.
jeroenh
@jeroen: post updated.
Hans Passant
@Hans @jeroen. In your edited code, `app` is inferred as type `Object`, so it's not possible to *use* the Excel Application object `app`. For instance if I substitute `app.Calculate` where you have `REM etc...` it doesn't compile. I think this is the problem jeroen is asking about. The compiler says `Error 1 Option Strict On disallows late binding.`
MarkJ
@MarkJ - you are absolutely right. I checked the VB.NET version 10 Language Specification to be sure. Shockingly the DLR is not mentioned *anywhere*. Option Strict Off looks completely unavoidable. My apologies for the poor answer.
Hans Passant
@Hans It's easily overlooked. The MSDN documentation really downplays the necessity of using `Option Strict Off`. Apparently it's possible to write a whole [walkthrough](http://msdn.microsoft.com/en-us/library/ee461504.aspx) without mentioning it, and only mention it briefly in a comment in sample code when [discussing early and late binding](http://msdn.microsoft.com/en-us/library/0tcf61s1.aspx). I think perhaps they are embarassed?
MarkJ
I was inspired to post a suggestion on Microsoft Connect that VB should have something like `dynamic`. Anyone who agrees or disagrees strongly can vote or comment on [Microsoft Connect here](https://connect.microsoft.com/VisualStudio/feedback/details/591963/please-give-vb-net-some-equivalent-for-c-dynamic-with-option-strict-on)
MarkJ
@MarkJ doubt whether this will get through, but it's certainly worth a shot
jeroenh