I need to switch from C# to VB.Net...
I always claimed that the discussion about C# being a better language compared to VB.Net was nonsense. In fact, I argued that it really doesn't matter. But after almost four years of developing C# applications, I think I'm starting to favour C#. That may sound late, after four years, but I hardly ever did anything in VB.Net, until now.
In 1989, when 20Mb was enormous for a harddisk drive, I switched from Basic to C. Yes, it's not a typo, regular Kernighan-Ritchie C. It drove me bezerk for a few weeks, but after that I really felt at home. In 1996, I made the switch from C to VB6. The same thing happened, it took me some weeks to get used to it, but after that I was fine. When I made the move to C# a few years back, I had no problems. It just felt right from the beginning.
For my current assignment, the applications need to be developed in VB.Net. I've been doing C# projects for the last couple of years and I'm having a hard time making the switch again. I've even installed SharpDevelop to help me. For those of you that never heard of SharpDevelop; SharpDevelop is a free IDE for C# and VB.Net which features a pretty good conversion tool between these two languages. The first application I built was written by me in C# and then converted to VB.Net using SharpDevelop. This works fine, although you need to solve some stuff.
But what strikes me most is the clean syntax that C# offers, compared to VB.Net. Of cource, C# was written from the ground up for .Net, whereas VB.Net had to be somewhat compatible with old versions of VB. But have a look at the following piece of code which reads a text file and stores the informaton in a database. Assume MyDataSet is a typed DataSet and UpdateDataSet stores the dataset information in the database.
private void Process(string fileName, string connectionString)
{ using (FileStream importStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
{ using (StreamReader importReader = new StreamReader(importStream))
{ using (SqlConnection con = new SqlConnection(connectionString))
{ using (DataSet ds = new MyDataSet())
{ string line = null;
while ((line = importReader.ReadLine()) != null)
{ ProcessLine(line, ds);
}
UpdateDataSet(connectionString, ds);
}
}
}
}
}
This C# code looks pretty clean and is quite easy to understand. But have a look at the VB version of this method:
Private Sub Process(ByVal fileName As String, ByVal connectionString As String)
' Using
Dim importStream As FileStream = New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None)
Try
' Using
Dim importReader As StreamReader = New StreamReader(importStream)
Try
' Using
Dim con As SqlConnection = New SqlConnection(connectionString)
Try
' Using
Dim ds As DataSet = New MyDataSet
Try
Dim line As String = importReader.ReadLine()
While Not (line Is Nothing)
ProcessLine(line, ds)
line = importReader.ReadLine()
End While
UpdateDataSet(connectionString, ds)
Finally
CType(ds, IDisposable).Dispose()
End Try
Finally
CType(con, IDisposable).Dispose()
End Try
Finally
CType(importReader, IDisposable).Dispose()
End Try
Finally
CType(importStream, IDisposable).Dispose()
End Try
End Sub
It's not bad, but I think it doesn't look as clean as the C# version. And what I found even more annoying is the fact that VB.Net is not case sensitive, or that it doesn't immediately compile because I type a semi-colon (;) at the end of each line. Although I've been told that VB.Net afficinados actually like this about the language.
Another example I just cannot get used to yet. I use interface and base classes in my projects. In C#, I just list the base class and interface I want to implement:
public class NewImport : ImportSetting, IDisposable
In VB.Net I have to explain what they are?
Public Class NewImport
Inherits ImportSetting
Implements IDisposable
I know, it's probably just me. But I'm having a hard time getting used to these differences at this moment. In a few weeks time, I'll probably be OK. But for now, I think I prefer C# over VB.Net. Mind you, I'm not saying VB.Net is worse than C#. I just feel more at home in C#.
I wonder what other peoples experiences are, switching between these two languages.