Home » Advanced CSharp » Using VB.NET From CSharp

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

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • .Net String Pool – Not Just For The Compiler.Net String Pool – Not Just For The Compiler On Monday, I was corrected in my assertion that creating multiple empty strings would create multiple objects.  Turns out the compiler automatically puts all of the strings that are […]
  • Copy And Paste And BugsCopy And Paste And Bugs We all do it.  I’m sure of it.  It’s too easy. I need code that looks almost like something else I wrote so I just copy and paste it over to the new code.  Done. But at […]
  • Object Initialization in CSharp 3.0 and VB.NET 9Object Initialization in CSharp 3.0 and VB.NET 9 Yesterday we looked at the new var keyword in CSharp.  This makes CSharp variable declaration similar to VB.  After all, they've had the DIM keyword for years which essentially does the […]
  • Which Language VB or CSharp?Which Language VB or CSharp? I wrote the following article a long time ago on my main domain, but recently I had a reader send in a question that this article addresses.  So I’m putting this article here so that it is […]
  • Cross Language References in ASP.NETCross Language References in ASP.NET Most ASP.NET programmers are aware that the environment allows programmers to write code in multiple languages.  This is what allows a programmer who prefers CSharp to write modules […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

  • http://www.ferienwohnungsizilien.com/ Ferienwohnung Sizilien

    This is awesome information, with this things to do will be much be easier, I will explore Csharp more …thanks.. looking for more bright ideas from you…