Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Visual Studio - C# Making Use of Visual Basic Namespace


  • Please log in to reply
No replies to this topic

#1 chili5


    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 26 August 2011 - 12:53 PM

Visual Studio 2010 – Making Use of Visual Basic.NET namespace

In some situations certain classes that you need may not be presently available to you. To get access to the classes you need you can write the classes you need if they do not exist. A lot of the time though the method you need does exist in the .NET library and you simply need to add a reference to the appropriate code to make use of it. Since part of the .NET library is the Visual Basic language you can actually make use of the VB.NET language in C# programs just by adding a reference to the Visual Basic namespace.

For instance, recently I needed a way to calculate the number of days between two dates. I browsed the C# libraries but could not find a method that did this for me. After some research I discovered in the VB.NET libraries was a method DateDiff that did what I needed to do. To make use of this method I had to do two things: add a reference in my project to the Microsoft.VisualBasic namespace and indicate that my class file is making use of the Microsoft.VisualBasic namespace.

Adding a Project Reference

In your solution explorer look for the references section like in this picture:


Right click references and click add references. You will be presented with a screen that looks like this:


Make sure that under assemblies that framework is selected. You will see a list of namespaces that you can reference. Ones that your project is referencing will be marked with a check mark. Confirm that beside Microsoft.CSharp is a check mark. This allows you to make use of C# classes in your C# project. If you are programming Visual Basic then adding Microsoft.CSharp will allow you to use C# classes in your VB project.
To add a reference to the Visual Basic namespace locate Microsoft.VisualBasic in the select box and double click it. A checkmark should appear beside Microsoft.VisualBasic like this:


Click ok and in the solution explorer expand the references drop down and in the list you should see Microsoft.VisualBasic:


When Microsoft.VisualBasic appears in the reference list this confirms that your project is correctly referencing the namespace. Now to make use of the namespace you need to include it in your code like this:

 using Microsoft.VisualBasic;

To demonstrate how to use Visual Basic methods we are going to show how to find the number of days between two dates.
First we create two dates:

DateTime start = new DateTime(2011,08,12);
DateTime end = new DateTime(2011, 09, 15);

The DateAndTime.DateDiff method takes three parameters: a DateInterval, a start date and an end date. The DateInterval is used to specify what difference you want. You might want to know the number of years between two dates or number of weeks. For this example we want to know the number of days. For this the first parameter we pass is DateInterval.Days.

Try this code:

long days = DateAndTime.DateDiff(DateInterval.Day,start, end);
Console.WriteLine("day difference: " + days);

The output is:

day difference: 34

If you look on a calendar you can confirm there are 34 days between these two dates. If end date is < start date then the value returned will be negative.

The process for adding more references and making use of their code is very similar to the process for the Microsoft.VisualBasic namespace.
  • 0

Also tagged with one or more of these keywords: namespace

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download