Back To Basics – Can we use both C# and VB Project within a single Visual Studio Solution ?

Back To Basics – Can we use both C# and VB Project within a single Visual Studio Solution ?

This is one of my favorite question during interview as a part of .NET Framework fundamental, and I generally received very mixed response. Can we have both c# and visual basic project in same solution ?.  The response  I used to received “No, it is not possible”, “Yes. we can do it for web project” etc. This question comes up when we discuss about very basic of CLS, CTS, MSIL etc. While developers having understanding with these terminologies, most of them failed to understand this basic question which actually related to them.

All right! So, what is the answer ? – Of Course ! Why not ? We can have a Solution, with different types of languages and they can refer. Just like a another reference library; in fact it could be any other language supported by the Framework. When they compiles, all the languages uses their native Compiler ( C# –> CSC Compiler, VB –> VB Compiler ) to generates the IL and that is understandable by other languages.

IL

 

Here is one simple walkthrough on using this..

  1. Run a new instance of Visual Studio
  2. Create a new C# Console Application Project.
  3. Add a new “Visual Basic Project” using “Add New Project” dialog window”
  4. Add the VB Class Library Project as Reference to C# Project.

This is how the solution should look like:

image

image

 

Now, you can write some dummy code block , one method that write something on console, and one method to add two number.

Public Class VBClass
Public Function VBMethod()
Console.WriteLine("VB Method Called")
End Function

Public Function AddNumber(ByVal num1 As Integer,
ByVal num2 As Integer) As Integer

Return num1 + num2
End Function
End Class

Here is the C# Code block, that calls the above VB methods which included as part of reference assembly.

class Program
{
static void Main(string[] args)
{
CSharpMethod();
VBClass vbClass = new VBClass();
vbClass.VBMethod();
Console.WriteLine(vbClass.AddNumber(5, 6).ToString());

}

public static void CSharpMethod()
{
Console.WriteLine("C# Method Called");
}
}

Once you run the application, this is how your output will come.

image

The application execution code map would look like as below.

image

To take an inside look, if you open both the VB Library, C#  Code using ILDASM,now;  you should be able to relate how IL code is getting called with same data type over different language.

image image

Hope this will give you a good understanding.

 

How To Do it for ASP.NET Web Application  ?    A Beginner’s Guide to ASP.NET Application Folders

DON'T MISS OUT!
Subscribe To Newsletter For your Daily Development Tips and Tricks  

 Best Tips & Tricks on Microsoft Technology Platform 

Invalid email address
We promise not to spam you. You can unsubscribe at any time.
Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

4 Comments to “Back To Basics – Can we use both C# and VB Project within a single Visual Studio Solution ?”

Comments are closed.