locked
Dynamic Keyword equivalent in VB.Net?

    Question

  • Ok i seen another answer similar question to mine but it didn't go further to answer my real question so here goes. The "Dynamic" keyword in c# 4.0 is a great feature. It looks like there is no equivalent in vb.net. In the other thread people suggesting just turning option strict off and you would, in essence, be doing the same thing. I see two problems with this:

    1) Most people are going to want to turn option strict off the the entire project just want to do it in a couple places
    2) What i'm trying to do is return / accept anonymous types.

    My question really is around point 2 above. For example you can return results from LINQ and not worry that the return could potentially have a different structure. I tested this with dynamic & c# and it worked great. This is probably what i will use dynamic keyword for the most in C#. Is there any way to do this in vb.net that i'm not seeing?

    thanks,
    Choclabs
    Wednesday, March 03, 2010 6:08 PM

Answers

  • There is not an equivalent VB keyword at this time.  Those who are advising you to juggle the Option settings may not fully understand all that the dynamic keyword does.  It allows to resolve problems such as generic covariance casting  exceptions.  For example.....

    Public Class Class1
    
        Sub TestMethod()
            Dim list1 As List(Of IInterface) = New List(Of IInterface)() ' this works
            list1.Add(New BaseClass()) ' this works, you can add a BaseClass objects
            Dim list2 As List(Of IInterface) = New List(Of BaseClass)() ' this does not
            list2.Add(New BaseClass()) ' this does not, you should add a BaseClass objects
    
        End Sub
    
    End Class
    
    Interface IInterface
    
    End Interface
    
    Class BaseClass : Implements IInterface
    
    End Class
    
    Class Consumer : Inherits BaseClass
    
    End Class


    Rudy  =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    • Edited by Rudedog2MVP Wednesday, March 03, 2010 6:40 PM
    • Marked as answer by Choclabs74 Wednesday, March 10, 2010 6:53 PM
    Wednesday, March 03, 2010 6:27 PM

All replies

  • There is not an equivalent VB keyword at this time.  Those who are advising you to juggle the Option settings may not fully understand all that the dynamic keyword does.  It allows to resolve problems such as generic covariance casting  exceptions.  For example.....

    Public Class Class1
    
        Sub TestMethod()
            Dim list1 As List(Of IInterface) = New List(Of IInterface)() ' this works
            list1.Add(New BaseClass()) ' this works, you can add a BaseClass objects
            Dim list2 As List(Of IInterface) = New List(Of BaseClass)() ' this does not
            list2.Add(New BaseClass()) ' this does not, you should add a BaseClass objects
    
        End Sub
    
    End Class
    
    Interface IInterface
    
    End Interface
    
    Class BaseClass : Implements IInterface
    
    End Class
    
    Class Consumer : Inherits BaseClass
    
    End Class


    Rudy  =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    • Edited by Rudedog2MVP Wednesday, March 03, 2010 6:40 PM
    • Marked as answer by Choclabs74 Wednesday, March 10, 2010 6:53 PM
    Wednesday, March 03, 2010 6:27 PM
  • Here's a clearer example of generic covariance.


        Sub TestMethod_02()
            Dim obj1 As BaseClass = New BaseClass()
            Dim obj2 As IInterface = CType(obj1, IInterface)
            '
            '
            Dim list1 As List(Of BaseClass) = New List(Of BaseClass)()
            Dim List2 As List(Of IInterface) = CType(list1, List(Of IInterface))
        End Sub



    Those 2 sections of code are very similar. 
    Casting the "obj#" objects is easy and straightforward.
    Casting the "list#" objects is prohibited by the compiler.
    This second scenario where the dynamic keyword is used to defer type resolution until run time.
    Mark the best replies as answers. "Fooling computers since 1971."
    Wednesday, March 03, 2010 6:38 PM
  • For #2, the "var" keyword is what you'd normally use in C# for this - are you saying that you have a situation where the compiler cannot detect the LINQ result type?  Wherever "var" works in C#, inferred typing will work in VB (omitting the type with Option Infer On).

    If you definitely need "dynamic" in C#, then Option Strict Off in VB used in conjunction with the "Object" type should work in the same way.

    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Wednesday, March 03, 2010 10:43 PM
  • To see examples of what David was talking about ... I have several on my blog. I have both C# code that uses var and VB code that uses Dim without a data type.

    http://msmvps.com/blogs/deborahk/archive/2009/10/11/lambda-expressions-syntax.aspx

    Hope this helps.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Wednesday, March 03, 2010 11:18 PM
  • Check out 
    CTypeDynamic()
    

    and the

    System.Dynamic()
    object namespace in .NET 4. It should give you everything you need.

    Friday, December 17, 2010 3:54 PM
  • You can define the variable as Object.
    Thursday, January 20, 2011 2:09 PM
  • You can define the variable as Object.


    And you also need to throw type safety out the window by setting Option Strict Off.

    Option Strict Off

     

    Module Module2

     

        Public Sub DynamicObjectDemo()

            Dim obj As Object = 3

            obj = obj + 3  'Requires Option Strict Off

        End Sub

     

    End Module

     

     

    If that is your wish, then proceed with what best meets your needs.  The dynamic keyword allows developers to maintain type safety at compile time with the rest of their code.  To my knowledge that degree of selectivity is not currently implement in Visual Basic.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

     
    Thursday, January 20, 2011 10:42 PM