Home

Using Microsoft Visual Basic Functions

 

Description

One of the strengths of Visual Basic, from its beginning, was its huge library of functions. Unfortunately, even when Visual Basic was part of the Visual Studio 6.0 environment, its functions belonged only to it and to its child languages such as VBA and VBScript. When Visual Studio .NET was created, the developers of Visual Basic added all of its valuable functions and in fact made them available to the other languages that use the .NET Framework. This means that those wonderful functions are available to use in your C# programs.

The functions of Microsoft Visual Basic still belong to it and they can be called transparently in a Visual Basic application. If you want to use them in a non-Visual Basic application, you must remember to reference its library. Most (if not all) of the functions of Visual Basic are created in the Microsoft.VisualBasic.dll assembly but they might be in different namespaces. Based on this, you can include any Visual Basic function in your program. Here is an example:

Source File: Exercise.cs
using System;

class Exercise
{
    static void Main()
    {
	double Number;
	double Result;

	Console.Write("Enter a number: ");
	string strNbr = Console.ReadLine();
	
	if( !Microsoft.VisualBasic.Information.IsNumeric(strNbr) )
	    Number = 0.00;
	else
	    Number = Microsoft.VisualBasic.Conversion.Val(strNbr);

	Result = Number * 2;

	Console.WriteLine("{0} * 2 = {1}", Number, Result);
    }
}

When compiling the program, you must reference the Microsoft.VisualBasic.dll library. Here is an example:

csc /reference:Microsoft.VisualBasic.dll Exercise.cs

 

 

Home Copyright © 2006-2016, FunctionX, Inc.