Vb.Net Equivalent for C# 'Dynamic' with Option Strict On

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

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

Is there an equivalent of dynamic type in VB.NET or other way to achieve the same behavior?

No equivalent for dynamic in VB.NET, the return type must be Object. There is no support in the CLR either, but all that is really needed here is to convince the C# compiler that "Object" must be interpreted as "dynamic". In other words, you need to do in VB.NET what the C# compiler does to annotate the return type in the metadata. That merely takes an attribute, like this:

Public Function findById(id As String) As <System.Runtime.CompilerServices.Dynamic> Object
Return session.FindById(id)
End Function

.NET 4.0 framework dynamic features in VB with Option Strict On?

It appears you can't without having to turn Option Strict off.
I'll research some more though.


Edit


After going through some documentation on the ExpandoObject, it appears it is used in C# for COM and Office Interop. Traditionally, in VB.NET, the Object was used for such purposes and that would require you turn off Option Strict.

To answer your question this means that you can use dynamic types in VB.NET by using the Object type instead of the ExpandoObject [if such a type exists in VB.NET], set Option Infer On and Option Strict On or Off.

You could also consider using partial classes to localise your non Option Strict code to particular files.







Suggested Reading

  • Dynamic Type in C#, Equivalent in VB
  • Using Type Dynamic (C# Programming Guide)

Option Strict On disallows implicit conversions from 'String ' to 'Char' VB.NET

Please continue to use Option Strict On. It is annoying but it will save your day a lot.

For your problem:

It is caused by the fact that when you enable Option Strict On, the compiler is no longer allowed to take the first char from your string and use it as separator. Because there is no overload for string.Split that takes just a string, then it complains about an attempt to an do invalid conversion.

If you want to use a string as separator then it is required to pass an array of strings as the first parameter, and a second parameter of type StringSplitOptions is required.

Fixing it is really simple. Just change the line to:

For Each entry In returnedString.Split({".exe"}, StringSplitOptions.None)

Enforce datatypes passed to methods (Option Strict)

Selectively Turn on Option Strict

Add to the top of the code file:

Option Strict On

Since this will enforce the option only in those files, this method is not recommended on an ongoing basis because it requires you to remember to do so for each file.

Project Wide

Open Project Properties (Project Menu - Properties among others). On the Compile Tab set the Option Strict drop down to On. You can also demote some conditions from an error to a warning or ignore that condition.

This too can be forgotten.

Visual Studio Default (recommended)

You can make Option Strict On the default setting for all new projects, so you don't have to remember to do so at any level:

Tools Menu -> Projects and Solutions -> VB Defaults

This wont affect a project already in the works - use #2 for that.



Related Topics



Leave a reply



Submit