2

i have one application which is build using VB.NET now

i required to add some C# code inside my existing VB.NET application does it possible or not?

6 Answers 6

9

Create a seperate project for the C# code. Then place a reference to C# project into the VB.Net project. You can then access the C# objects as if they were written in VB.Net

Sign up to request clarification or add additional context in comments.

Comments

3

You can't add different languages to the same project, but you should be able to add both projects to the same solution, and add a reference from your C# project to VB project or VB project to C# project, and use the types he's created - so long as they're public, of course.

Follow this approach:
1) Create Multi-Project Solutions

2) Add Reference of your CSharp project in your project

Then you can create object of those Class/Forms etc and access methods that you created in CSharp project as:

Dim obj As New YourCSharpProject.SomeClass()
obj.UpdateData()

Ref: Launch VB form from a C# form

Comments

2

No. You will need to create a separate project within your solution and place the code in that. If the project is a class library then your original project can reference that and call the library routines. Failing that, there are plenty of online C#<->VB.NET converters available.

Comments

2

Like others said, you cannot directly intermix the two languages because a compiler will only understand the language it is written for. Hence, a VB.NET compiler cannot understand C# code unless MS creates an extension to do so. Something like this comes from my imagination :) -

Dim v as int
%for(int i = 0; i < $v; ++i){}% 

3 Comments

something like below code snippet... static class Program { [DllImport("kernel32.dll")] static extern bool AllocConsole(); static void Main() { if (true) { //do some things, for example starting the service. } else { AllocConsole(); } } }
DllImport will expose methods from unmanaged code. Using that you can use c/c++ functions from your vb.net/c# code. Refer to this SO question: stackoverflow.com/questions/213045/…
Bottomline is that you cannot directly intermingle these two languages in a source file. You have to seperate (put in a DLL) and import (import the DLL by referencing it)
1

Yes, as long as it's a seperate class / page / webcontrol / usercontrol and it's specified as C#. Visual Studio will not like it, and the easy way in VS is to setup a new project within the solution.

3 Comments

Means if i have a complete separate C# class than i could call function of C# class files..from VB class file????? thank you so much for answering....
No you need a separate C# project. The only way I've seen of mixing languages within the same project is within the App_Code folder of an ASP.NET project.
Or if you're using a website, and just a "new web site..." then you can add classes, pages, usercontrols etc. of either language (which is where the App_Code folder comes into play).
1

If it's just a little C# code, you can first convert it to VB.NET with a converter like http://www.developerfusion.com/tools/convert/csharp-to-vb/, or you can convert it manually.

Otherwise, as already noted, you'll have to put the C# code in another project in the same solution. You can then call the C# code from your VB.NET code in the same solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.