Dude, where’s my VB.Net code?

It came to my attention that a large part of the LightSwitch community prefers VB.Net over C#.  I always thought that C# and VB.NET are close enough together in semantics that any VB.NET developer can read C# and vica versa.  However, I must admit that I looked at the VB.NET version of my own samples, and once there’s generics & optional parameters involved, the VB.Net version becomes significantly different compared to its C# brother. To overcome this language barrier, thank god there’s tools online like “Google Translate”, which for .NET code, is provided to us for free by Telerik.

A special thanks to Telerik, Todd Anglin in particular, for providing us with this awesome translating power!

I’ll promise to do my best to post my samples in both C# and VB.Net in the future.


Update (16:40): Falkao correctly indicated in the comment section that this part of the community might be quite large… Let’s find out what the exact % is among my blog readers…

20 thoughts on “Dude, where’s my VB.Net code?

  1. Your observation is quite correct. VB is mostly used in business applications and many of us are still using VB6. This community is quite large and when starting with LS they do prefer VB.Net.

  2. Thanks Falkao. I have 4 .Net developers here and we all use VB.Net and ASP.Net.
    We are a financial investment advisory firm and it just happens that we all are more comfortable working in VB.Net and when we have to do C# becasue a vendor wrote something in C# that we are allow to modify, it slows us down a lot. On the surface it would appear C# and VB are interchangable (sans the : ) but in fact dealing with inheritance and generics become increasingly hard as we are trying to learn the vendor’s code at the same time as translate it to VB.
    My most recent mindbender was learning the extensibility layer to a vendor’s code that is a WCF/WPF app written in C# and built on nHibernate. The xaml and WCF was not exposed, instead the vendor exposed class abstractions using a combination of nHibernate and C#. I could have written VB classes and inherited the classes, but in order to truly understand the code (I was doing due dilliengence on the app prior to purchase) I stayed in the native C# code. It was really, really slow going for me.
    Having VB samples just removes the obsticle to my understanding the concept being presented.
    Getting off his soapbox<—Thank-you for consdiering this Jan, and thanks again.

    • Hey Dan!

      Yes I shared the opinion that VB.NET and C# are pretty much interchangeable… I mean I understood most VB.NET code samples I have seen so far. However this indeed became void really quickly once you go into generics, inheritance, …

      OTOH if your vendor wrote crap code, it’ll be hard to understand in C# and VB alike 😉

      Thanks for your opinion & thanks for shouting out on my blog! The line “Having VB samples just removes the obsticle to my understanding the concept being presented.” in particular, was spot on…

      Expect the next code sample to be in VB! It’ll be a challenge I’ll gladly accept.

  3. Jan,
    I used the Telerik Code translator to convert code from C# to VB. It did a great job.
    Then I created a solution and copied the non-code assets over to the new VB solution.
    I would like to zip up the solution and mail it back to you but I am at a show stopper right now.
    In VB you do not monitor forProperyChanged, you RaiseEvent instead and when I tried to implement a RaiseEvent in INotifyPropertyChanged, I realized this was what your class is doing and I had a hard time trying to create the RaiseEvent without conflicting with your Base Class and I was not sure how to monitor for changes to raiseEventwithout using your base classes in doing so.
    The line in question is “Me.navigationModel.PropertyChanged ”
    Here is the converted code, without any changes made by myself:

    Imports System
    Imports ExtensionsMadeEasy.ClientAPI.Navigation
    Imports Microsoft.LightSwitch.Client
    Imports Microsoft.VisualStudio.ExtensibilityHosting

    Namespace LightSwitchApplication
    Public Class NavigateBackCommand
    Inherits ExtensionsMadeEasy.ClientAPI.Commands.EasyCommandExporter
    Private ReadOnly navigationModel As IEasyNavigationModel

    Public Sub New()
    MyBase.New(“Back”, “Click to go back”, “Navigation”, New Uri(“/NavigationDemo.Client;component/Resources/backward.png”, UriKind.Relative))
    ‘Step 1: getting the navigation model
    Me.navigationModel = VsExportProviderService.GetExportedValue(Of IEasyNavigationModel)()

    ‘Step 2: start listening to “Can go backward” changes…
    ‘DanG – This is the line that will not build.
    Me.navigationModel.PropertyChanged += New System.ComponentModel.PropertyChangedEventHandler(navigationModel_PropertyChanged)
    Me.IsEnabled = Me.navigationModel.CanNavigateBackward
    End Sub

    Public Overrides Sub Execute(currentScreen As IScreenObject)
    ‘Step 4: navigating backwards
    End Sub

    Private Sub navigationModel_PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs)
    ‘Step 3: keeping the command “Can go backward” in sync with the navigation model…
    Me.IsEnabled = Me.navigationModel.CanNavigateBackward
    End Sub
    End Class
    End Namespace

    ‘Service provided by Telerik (www.telerik.com)
    ‘Conversion powered by NRefactory.
    ‘Twitter: @telerik, @toddanglin
    ‘Facebook: facebook.com/telerik

  4. Hi Dan,

    I believe the line that has
    “New System.ComponentModel.PropertyChangedEventHandler navigationModel_PropertyChanged)”
    needs to be
    New System.ComponentModel.PropertyChangedEventHandler(AddressOf navigationModel_PropertyChanged)

    None of the available “translators” seem to get everything right, all the time. I’ve recently had an example of some VB code that I needed translated (with a different translator than Telerik), but the translated code wouldn’t compile.

    I hope that what I’ve suggested above helps.


  5. Jan,
    I don’t think I made it clear before. I was talking about the EventPropertyChanged event. VB handles this through a RaiseEvent and does not allow you to call it directly like you can in C# (not that I know of…). This is the error on the build (and BTW is the last of the Build errors, after it is fixed, I will zip up the solution for you as there were about a dozen small changes to switch to VB).

    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs)’ is an event, and cannot be called directly. Use a ‘RaiseEvent’ statement to raise an event. C:\ Projects\NavigationDemo\Client\NavigateBackCommand.vb 19 13 Client

    • Hey DanG,

      I was writing a blog post about events (both in C# AND VB), at the same time as you guys having this discussion 🙂

      I’m a bit lost on how well your conversion is working, but I must say that that writing that blog post in two languages has thought me LOADS… A good exercise! I hope it’s of some learning value to you too.

  6. LS has great appeal to the new to programming community, like myself and I think people learning this for the first time will go with C#. Therefore, the need for code samples is greater than VB who seem to be old hats at programming

    • Lol at the old hats remark 😮

      According to the stats so far, the need for C# samples is about twice as large as the need for VB samples. Still doesn’t mean that the latter is negligible…

  7. Sorry, I did not look to see who replied Yann. I thought I was replying to Jan.

    Bhuven, I an a very old hat when it comes to VB, started in 1996 with VB6. If I were starting now, I would definitely pick C# as the starting point as it has greater market value than VB.

    • MY GOD!
      So far, during my first contact with VB, I already found it quite the challenge to do lambda expressions, events, even the syntax for expressing basic inheritance. I’m not sure if I ever want to face the VB conversion of “yield return”… WHOA!

      (What was it by the way? 🙂 )

      • well, with help of Justin Anderson from LS team:

        “You can use collection initialization for a somewhat equivalent of the yield return keywords of C#:

        Public ReadOnly Property Commands As IEnumerable(Of IShellCommand)
        Return New List(Of IShellCommand) From {New MyCommand()}
        End Get
        End Property

        Here’s information from MSDN on collection initializers in Visual Basic: http://msdn.microsoft.com/en-us/library/dd293617.aspx

  8. I don’t think one can say which one is better. Both do compile to the same CIL and hence I guess all is primarily related to taste and background.

    You still can use some VB6 coding style in VB.Net and there are some features that are missing in C# like the “With end With statement”, runtime COM and interop, WithEvents and Handles constructs. Therefore some of the C# code might not convert.

    When checking samples you’ll notice that the more hardcore ones are in C#, like in control coding or XNA gaming. When checking business samples you’ll get mostly VB.Net ones. For LS samples it depends. Beth for example posts mostly in VB.Net whereas the more technical ones are in C#.
    personally do prefer VB.Net (as it looks better), but I don’t have any problems with converting code except with some specific constructs.

  9. In my former life I was a Flex actionscript developer, so to me C# is closer to what I’m used to programming in, plus I already had a book on C# I’d purchased a few years back 🙂

    I’m also not opposed to either learning or converting VB.

  10. Pingback: Extensions Made Easy v1.12: Fuzzy searching « Jan Van der Haegen's blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s