Jan Schreuder on .Net

.Net code samples, experiences, observations

View my professional profile on LinkedIn

Recent Posts

Tags

News

  • Inappropriate comments will be deleted at my discretion.

    The information and code samples in this weblog is provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the merchantability and/or fitness for a particular purpose.

Community

Email Notifications

Tool suppliers

Tools

General

Microsoft

Favorite blogs

Archives

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.

Comments

Patrick Wellink said:

Well....

For sure i have to kick in here.......
The using stuff uyou are using is translated in try block to get the same functionality in VB.Net.

But you can do without those in vb and clean up the system in a different way. By doing so your code would look a lot cleaner....

and btw the stuff that is doing really something ....

                  while ((line = importReader.ReadLine()) != null)
                   {
                       ProcessLine(line, ds);
                   }
                   UpdateDataSet(connectionString, ds);

and

                   While Not (line Is Nothing)
                       ProcessLine(line, ds)
                       line = importReader.ReadLine()
                   End While
                   UpdateDataSet(connectionString, ds)

are not that different at all....
and I think Nothing is clearer then NULL

Also i likw writing out stuff, i really do like writing out Inherits and implements....



# July 10, 2006 6:58 AM

Jan Schreuder said:

I knew you would respond :-P. The using stuff in C# is great. You just know that the Dispose method on those objects is called. Why that is a good thing can be found here: http://bloggingabout.net/blogs/jschreuder/archive/2006/01/19/10814.aspx. And VB.Net 2.0 also has the Using statement.

But it's a matter of taste, I guess. I don't think VB.Net is lesser language, I just prefer C#.
# July 10, 2006 7:13 AM

Ramon Smits said:

Did you know that you could omit al those brackets? It makes the c# version even more attractive :)

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);
   }
}
# July 10, 2006 11:20 AM

Jan Schreuder said:

Of course, Ramon. But I always add brackets, even for if statements where only one line of code follows. A matter of taste, I guess :-)
# July 10, 2006 11:24 AM

Rick van den Bosch said:

[quote]Of course, Ramon. But I always add brackets, even for if statements where only one line of code follows. A matter of taste, I guess :-)[/quote]

And that's exactly my taste! In Dutch: Hulde! On-topic: I think C# is a bit cleaner too. That's probably because of being around C# (or not being around VB.Net) for a long time. At the end of the day there's one important question: does it work? And if it does: party! ;)
# July 10, 2006 1:10 PM

Ramon Smits said:

@Jan & Rick: It depends. When I have an if statement with one line then I put the whole comparison on one line:

if(x) return 27;

I always put if, switch, do, while blocks seperate with the previous and upcoming code. The result is very clean and readable code. The using block in my example is very readable although not all using statements have their own brackets.
# July 12, 2006 5:25 AM

Jan Schreuder said:

Ramon, don't get me wrong. I'm not critisizing your style of coding. Your code sample is very clean and readable. I just wanted to point out that I, as a rule, almost always add brackets. That goes back to the time that I was building C apps. I do have situations where I, like you, place the one-line-if-statement on one line. But that's about the only exception I can think of. A matter of taste.

I do the same as you for if, while, etc...
# July 12, 2006 5:47 AM

Daniel said:

FYI- I didn't realize until watching dnrTV, but Using is available in VB as of .NET 2.0.  That used to be one of my reasons for preferring C#, too. =)
# July 13, 2006 3:01 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 8 and 1 and type the answer here: