Lazycoder

10Aug/041

5 lines of VB.NET to one line of C#

17 lines of C# to 1 line of VB.Net

I usually have the opposite problem, I have to type more in VB.NET than I do in C#. Take the instance of trying to figure out if a given type implements a specific interface. For C# I have the “is” keyword.

C#


if(ctrl is IPlugin)
     _loadedPlugins.Add(ctrl);
</code>

VB.NET
<code>
            For Each InterfaceName As System.Type In ctrl.GetType().GetInterfaces()
                If InterfaceName.Equals(GetType(IPlugin)) Then
                    _loadedPlugins.Add(ctrl)
                End If
            Next

If anyone knows of a better way to do this, please post it in the comments.

update: Dean points out the “typeof()” function in VB.NET that does the same thing that “is” does in C#. Thanks Dean!

Filed under: General Leave a comment
  • dean

    use the vb “typeof” statement:
    if typeof(ctrl) is IPlugin then _
    _loadedPlugins.Add(directcast(ctrl, IPlugin )