Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET equivalent for C# 'dynamic' with Option Strict On

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

like image 242
jeroenh Avatar asked May 22 '10 22:05

jeroenh


People also ask

How similar is VB.NET c#?

Though C# and VB.NET are syntactically very different, that is where the differences mostly end. Microsoft developed both of these languages to be part of the same . NET Framework development platform. They are both developed, managed, and supported by the same language development team at Microsoft.

What is operator C#?

Operators are symbols that are used to perform operations on operands. Operands may be variables and/or constants. For example, in 2+3 , + is an operator that is used to carry out addition operation, while 2 and 3 are operands. Operators are used to manipulate variables and values in a program.

What is equivalent to C#?

It is enharmonically equivalent to D-flat major. Its key signature has seven sharps.


2 Answers

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#.

like image 161
Darin Dimitrov Avatar answered Oct 18 '22 08:10

Darin Dimitrov


VB.NET always had the "dynamic" feature built in, originally called late binding. This syntax was supported forever:

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

Worked on code implemented in .NET and COM, the latter being the most common use. The dynamic keyword in C# gave it that same capability. It did get changed in VB.NET version 10 however, it is now using the DLR as well. Which adds support for dynamic binding to language implementations like Python and Ruby.

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# using a specific keyword to signal dynamic binding was a pretty good move. Afaik all requests to do so in VB.NET as well have as yet been considered but not planned.

If you prefer Option Strict On, then using the Partial Class keyword so you can move some of the code into another source file is probably the most effective approach.

like image 39
Hans Passant Avatar answered Oct 18 '22 10:10

Hans Passant