KnowDotNet Visual Organizer

Learn to Get By Without Microsoft.VisualBasic Namespace

Learn to use the .NET Namespaces

by Les Smith
Print this Article Discuss in Forums

Moving to C# from VB.NET or VB.NET from VB6.  You are probably still using the Microsoft.VisualBasic Namespace.  Wean yourself from it and learn to use the .NET Namespaces.  The Accompanying DLL is a lot of weight to carry for nothing.

Having programmed in Visual Basic for over 10 years, I find myself moving more and more to using C#.  I still do both, but in working with very large projects, C# has some definite advantages.  But, having used VB for so long, I found myself searching for functions that were in VB and VB.NET, via the Microsoft.VisualBasic DLL, that I could not figure out how to do in C#.  Of course, I have seen others use this DLL in C# by adding a reference to the DLL and adding "using Microsoft.VisualBasic, but this did not seem really cool for developing in C#.  So I began investigating some of the methods of existing Classes that I had never bothered to look at before.

For example, in VB, if I wanted to get the number of seconds since midnight, I would reference the Timer function with a line of code like the one shown below:

      Dim secs as Integer = Timer

The problem is that there is no such function in C#, and again, I did not want to bring Microsoft.VisualBasic into my C# program.  Although it works fine, it looked rathr amateurish.   Looking around in the DateTime class I found the following code that does the same thing. without the big VB DLL.  DateTime is part of the System Namespace, therefore all applications have this name space in them to start with.

   int secs = DateTime.Now.TimeOfDay.TotalSeconds;

By the same methodology, I can get the number of minutes since midnight.

   int mins = DateTime.Now.TimeOfDay.TotalMinutes;

Again, being strictly a VB programmer until two years ago, I constantantly used functions like DateAdd, which again is not part of C#, and is only part of VB.NET because when you create a VB.NET application using the standard template, Microsoft.VisualBasic is automatically added to your project.  But, there are even more powerful methods built into the .NET DateTime class.  For example, if I wanted to add a day to a date in VB.NET, I would use the following code.

   Dim tomorrow As DateTime = DateAdd(DateInterval.Day, 1, Today)

But, using .NET DateTime class, in either VB.NET or in C#, the code is simplified as shown below.

   DateTime tomorrow = DateTime.Today.AddDays(1);

or in VB.NET

   Dim tomorrow As DateTime = DateTime.Today.AddDays(1)

Obviously, there are many such methods as AddSeconds(), AddMinutes, AddYears, etc.

There are myriad other areas that could be touched on here, but just for kicks and to humor my partners, Bill Ryan and Brian Davis, I will give one more example.  For years, to the chagrin of Brian, I used the Val() function to convert a string to integer.  To a VB programmer, Val() is a handy function that will convert all digits of a string to integer until it reaches a non-integer character, at which point it stops.  But, guess what, the Val() function does not appear in C#.  What do I do?  Well, you have several options.

   int val = int.Parse("123");
  
int val = Convert.ToInt32("123");

You need to use care when using int.Parse (Integer.Parse in VB.NET).  The old Val() function in VB was fairly bullet proof, but int.Parse has options to tell it not to throw exceptions.  For example, the following line of code will raise a format exception.

   int val = int.Parse("123,456")

because of the existence of the comma.  However, using an overloaded parameter, you can tell the Parse to ignore the comma as follows.

   int val = int.Parse("123,456", NumberStyles.AllowThousands, MyCultureInfo);

The result will be 123456.

Well, that should be enough to whet your appetite if you are a VB programmer trying to get started with C#.  .NET is different, but I love it and would never go back to VB6.

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing