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.
Here is one simple walkthrough on using this..
- Run a new instance of Visual Studio
- Create a new C# Console Application Project.
- Add a new “Visual Basic Project” using “Add New Project” dialog window”
- Add the VB Class Library Project as Reference to C# Project.
This is how the solution should look like:
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.
The application execution code map would look like as below.
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.
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
Pingback: Visual Studio Enterprise - The Daily Six Pack: May 28, 2015
Pingback: Dew Drop – May 28, 2015 (#2023) | Morning Dew
What’s a “Complier” ? 🙂
It was typo of Compiler 🙂 . Thanks Fixed !