Using VB.NET From CSharp

G08L0047 Back when I was teaching .NET to other programmers regularly, I would frequently get the following question from a VB6 programmer moving to CSharp.

“Is there any way of using some of the VB functions I’m used to using within CSharp?”

The answer to this question has two parts, which we will explore today.

The first part to this question is, “I can’t remember the last time I NEEDED to do this.”  Actually, I’ve never needed to do this.  It might have made my life a little easier to replace 3 lines of code with one VB call, but I’m so used to programming in CSharp now that I hardly even notice.

However, the point of this article is not so much that you might need to do this as that you can.  In fact, you can use functionality from any .NET language from any other .NET language using a similar approach.

The first thing you’ll want to do is to add a reference to the language library for the language you want to use in your code.  In the case of VB.NET, you’ll load the Microsoft.VisualBasic.* assemblies

image

Next, you’ll want to browse the “Class View” to find out where each of the functions are.  By doing this, I can see that the MsgBox function is in the “Interaction” class inside of the Microsoft.VisualBasic namespace located in the Microsoft.VisualBasic assembly

image

So to use this function, I add a using statement at the top of my code to use the Microsoft.VisualBasic namespace and call the MsgBox function through the Interaction class

using Microsoft.VisualBasic;

Interaction.MsgBox("Message goes here",
    MsgBoxStyle.OkCancel, "Title goes here");

 


Other post in Advanced CSharp

Other Related Items:

Bdg Publishing Mastering Visual Basic .NET (Windows/Macintosh)Bdg Publishing Mastering Visual Basic .NET (Windows/Macintosh)Mastering Visual Basic .NET shows you what you need to know, to become a skilled .NET developer! Learn how to create a wide variety of applications in VB.NET with this great learning tool!
3 Shelf Bookcase - Cherry3 Shelf Bookcase - CherryCORSICA BOOKCASES 2-Shelf Bookcase Bookcases are manufactured using North American AA-grade hardwood, hand-selected veneers to ensure matching grain... Read More >
LINQ in ActionLINQ in Action

LLINQ, Language INtegrated Query, is a new extension to the Visual Basic and C# programming languages designed to simplify data queries and databas... Read More >

If you're new here, you may want to subscribe to the mailing list to get notifications of new post and a virtual tour of past topics. Thanks for visiting!

Related Post

One Response to “Using VB.NET From CSharp”

DotNetNuke Sponsor

 

Most Valuable Blogger
Sponsor