views:

489

answers:

12

I have a team with people that are quite comfortable in C# but we got a requirement to write a project in VB.net. How hard would it be to think in C# and on the fly convert to VB? Is that doable?

Could you list the issues that we can come across?

I heard that VB.net doesn't have closures. Is that still true for .net 3.5?

A: 

I don't think it would be too hard. VB.NET and C# are languages that are close to each other, only the syntax really differs. Of course it's going to take some time for your team to get used to the new language, but I don't think you'll run into big problems.

Oh, and VB.NET does have closures. It's missing a few other features from C# like the yield keyword, multi-statement lambdas and auto properties, but nothing very critical.

Meta-Knight
VB9 has one-line function closures. VB10 has the same closures as C#.
Strilanc
You have a good point; will edit.
Meta-Knight
@Strilanc, Lambdas and closures are separate concepts. The closure implementation for VB.Net is unchanged between 9.0 and 10.0. The lambda implementation varies greatly though.
JaredPar
That makes sense... edited again.
Meta-Knight
+12  A: 

If you are approaching VB.Net with the mindset of C# it's best to set the following options in the project

  • Option Strict On
  • Option Explicit On
  • Option Infer On

This essentially removes the late binding semantics of VB.Net and forces it to be a strictly typed language. This will make it closer to C# semantic wise (still not exact by any means).

VB.Net has Lambda Expression (and hence Closure) support starting with the Visual Studio 2008 / .Net Framework 3.5 release. Not expression and not Statement. Statement lambdas are not supported until VS2010 / .Net Framework 4.0. Although you can use the 4.0 compiler to downtarget 2.0 frameworks.

JaredPar
A: 

I think C# to VB.NET won't be too painful, it's just a case of learning a new syntax. In the current versions the capabilities of both languages are fairly closely aligned.

The other way round (VB.NET to C#) could be harder because people might be used to making use of the 'My' namespace and other things put in there to make VB6 developers feel at home.

AdamRalph
A: 

There are some subtle differences you'll have to watch out for. For example VB.Net has no concept of short circuiting an if statement (I was corrected that apparently it does). If it just a short term project, you probably won't have a problem, but different languages do have different approaches to solving the same problem. An example of this is Python programmers talking about doing things in the "pythonic" way. They talk about this concept in the book Dreaming in Code where java programmers were trying to program java using the python syntax. It leads to taking the long way around to solving a problem. Will this happen with C# and VB.Net? It is hard to say, they both use the underlying frame work, so the differences won't be huge, but it would still help to try and learn how to use VB.NET the way it was intended.

Edit: so apparently, it does has the concept of short circuiting, but it doesn't do this by default where C# does. This just further proves the point of learning how the language functions can be beneficial in the long term.

Kevin
ShortCircuiting is done with AndAlso, OrElse.
Strilanc
So guess you learn something new everyday.
Kevin
I'm happy that short circuting was brought into discussion. Everybody in my team would expect `if x IsNot Nothing And x.method()` would not throw a null pointer exception.
Piotr Czapla
The structure of the languages are not very different, way back they have a common ancestor in ALGOL. The C# way of writing code generally translates very well to VB.
Guffa
I understand what you are saying, but that statement could be made for a lot of languages. C++, C#, Java, the list goes on and on. They are more similar than comparing to Perl, but there is a divergence.
Kevin
+1  A: 

I find this to be a handy article in highlighting the differences. I'm a vb.net programmer, and this helps me figure out c# code, so I'm sure it will work the other way!

http://www.codeproject.com/KB/dotnet/vbnet%5Fc%5F%5Fdifference.aspx

ScottE
Amazingly, that leaves out the AddHandler and RemoveHandler for VB.NET vs "+=" and "-=" for C#. Another thing that was rough when going to VB.NET from C# was the "Handles" keyword which followed a method in VB, subscribing to an event.
John Fisher
Good point. It's also tricky going the C# direction with AutoEventWireup and no use of 'Handles'. This is a good article explaining things. http://odetocode.com/blogs/scott/archive/2006/02/16/2914.aspx
ScottE
+1  A: 

Just like any language (human or computer), you first learn to "translate in your head," then you eventually start "thinking" in the other language.

The quicker your team can make that leap, the better. So, do it the same way the experts tell you to learn a human language: real-world examples and immersion.

There are a few C# to VB.NET conversion utilities available online, so start by having the team write in C#, convert to VB.NET, and clean it up. (The conversion utilities vary in quality and have some limitations, especially with newer language features.)

Once they get the hang of the basic "grammar," drop them into VB.NET 100%.

I use both every day, often in different code windows at the same time, and have no problem context-switching or doing the "right thing" in each.

richardtallent
+1  A: 

One of the biggest issues I've found is the apparent verbosity of VB. It has all these big keywords like MustInherit, NotInheritable, MustOverride, etc., where C# just has things like sealed, abstract and virtual. You have to have an End to everything (End Sub, End Function, End While, End Namespace, End Class, etc.) And you have to explicitly mark read-only properties with the ReadOnly keyword; simply omitting the setter won't fly. Also remembering AndAlso and OrElse instead of the more intuitive (but non-short-circuiting) And and Or, and things like Is Nothing and IsNot Nothing instead of == null or != null.

None of these are necessarily problems with the language, but if you're accustomed to the relative simplicity of C#, VB code may look like a whole lot of extra stuff to you.

John M Gant
Yes, VB is definitely a bit more verbose, but if typing is the biggest part of your programming, you are not working with projects that are nearly challenging enough... :)
Guffa
VB.NET is chattier, but also more descriptive. If you're just starting in .NET it's nice to see "`Inherits BaseClass Implements IDisposable Implements ISomeInterface` than `: BaseClass IDisposable ISomeInterface`; especially when you're trying to get code to compile and something is simply out of order. After a while it gets to be annoying though and the shortcuts in C# become more and more appealing
STW
@Guffa +1, funny... although it's really not just the typing, it's the reading, understanding and refactoring.
John M Gant
@Guffa - nice comment. But it really is annoying. i've worked with large projects in both c# and vb.net. and the best case example i have for annoying typing is parameters for methods (aka subs and functions :)) public string SomeMethod(string var1, string var2, int var3) is easy. public Function SomeMethod(ByVal var1 As string, ByVal var2 As string, ByVal var3 As Integer) As string. Ouch. The ideas happen faster than i can type it out :)
Kamal
+3  A: 

As C# and VB.NET uses the same framework and compile to very similar IL code, you have a lot for free. Writing Basic syntax instead is not that hard.

The C# syntax is more aimed at showing what's going on, while the VB syntax often hides away some details, so a C# programmer is already familiar with some concepts that may not at all obvious to a VB programmer. In some ways learning C# is a better way to learn how VB works than to learn VB itself...

I frequently answer VB.NET questions in different forums mostly based on my C# knowledge, and I still haven't written anything more than short test programs in VB.NET myself.

There are of course some quirks to look out for in VB. Like the / operator for example that always converts both operands to double, or the = operand that uses VB specific comparison code rather than the comparison specified for the equality operator in the .NET classes.

Guffa
+1 I always tell people learning the .NET platform (especially those new to OOP) to learn C# first, and then move on to VB if necessary. Nothing wrong with VB, but for the reasons you mention C# gives you a clearer picture of what's going on in the framework IMO.
John M Gant
@jmgant: I disagree, VB.NET provides a lower barrier to entry than C# and gets you coding in the .NET world faster--although the reasons to stay in VB.NET quickly diminish. I usually advise to start with VB.NET to get up to speed, then make the jump to C# to pull the curtains back the rest of the way.
STW
Guffa
Good answer. I do the same as Guffa, but in reverse. I code in VB but answer C# questions on SO from time to time. Goes to show that the VB vs C# debate is futile because both languages are actually very close (and will get even closer in the future). You just cannot say that one language is bad (or good) without saying that the other also is ;-)
Meta-Knight
A: 

One option if you don't feel like writing the vb.net code is to write your project in C#, compile it, and use Reflector to see what the vb.net equivalent looks like. It all compiles down to MSIL anyway!

ScottE
It works, sometimes. The regenerated code is iffy at best and if you're talking about a potentially sizable class then it can be painful.
STW
@Yoooder, I've never seen this in anything I've worked with. I find it's a great learning tool.
ScottE
it's only worked for me on small samples; whenever I need to export a sizable assembly I find myself having to correct a fair amount. Perhaps it's specific to the version of the framework I'm working with
STW
Yes, I'm sure you have to make changes, especially in more complicated scenarios. For example, I'm not sure how static classes would translate into vb.net, or how optional parameters are handled in c# pre 4.0. At any rate, it's still very handy!
ScottE
@ScottE, static classes are most closely analogous to modules in VB. You can't instantiate modules, and all of their methods are automatically static (and can't be explicitly declared as such).
John M Gant
+1 to compensate for the needless downvote. This is a perfectly legitimate way to see what a corresponding bit of VB code might look like. Obviously this isn't a way of translating production code, but it's still a useful learning tool.
Adam Robinson
+1  A: 

One area that VB.NET tends to try and cover up is working with events; others have briefly touched on some of the differences, but here's a little more on them:

VB.NET provides a WithEvents keyword for fields that raise events. If the field is declared WithEvents then you can add a Handles field.Event to the end of a method whose signature is compatible with the event; that method will automatically be a delegate of the event without needing to manually AddHandler and RemoveHandler (+= and -=).

Private WithEvents SomeField
Public Sub SomeField_SomeEvent(sender as Object, e as EventArgs) Handles SomeField.SomeEvent
    Console.Writeline("SomeEvent occured")
End Sub

Event declarations and raising events are simplified a bit. VB.NET doesn't require that you check if an event is null prior to notifying listeners:

Public event SomeEvent as EventHandler(of SomeEventArg)
Public Sub SomeMethod()
    RaiseEvent SomeEvent(Me, new EventArgs)
End Sub

One "hidden" feature of events in VB.NET is accessing the underlying MulticastDelegate, to do something like GetInvocationList() Note: the event is named SomeEvent and the code to access the multicastdelegate calls an invisible field named SomeEventEvent:

Public event SomeEvent as EventHandler(of SomeEventArg)
Public Sub SomeMethod()
    // Note that SomeEvent's MulticastDelegate is accessed by appending
    // another "Event" to the end, this sample is redundant but accurate.
    // If the event was named ListChanged then it would be ListChangedEvent
    dim invocationList = SomeEventEvent.GetInvocationList()
End Sub
STW
The last bit has nothing to do with VB.NET per-se. That's a framework feature; there isn't even any VB-specific syntax.
Adam Robinson
@Adam: the VB.NET specific portion is appending "Event" to the event name in order to access the multicast delegate.
STW
A: 

There were some useful articles in Visual Studio magazine back in Jan 2008.

You might also be interested in the question "what's allowed in VB that is prohibited in C# (or vice versa)"

MarkJ
A: 

Apart from what Jared has already mentioned you should have no problems in doing this. The only other source of irritation is weird defaults. E.G. Did you know that VB.NET projects, by default hide the references node in solution explorer? (you have to select ShowAllFiles to see it).

Quibblesome