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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Pritesh
  • 3,208
  • 9
  • 51
  • 70

6 Answers6

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

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
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

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
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.

deadlyvices
  • 873
  • 6
  • 18
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){}% 
badmaash
  • 4,775
  • 7
  • 46
  • 61
  • 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(); } } }` – Pritesh Apr 04 '12 at 11:18
  • 1
    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: http://stackoverflow.com/questions/213045/how-can-i-call-c-sharp-extension-methods-in-vb-code – badmaash Apr 04 '12 at 12:23
  • 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) – badmaash Apr 04 '12 at 12:26
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.

RemarkLima
  • 11,639
  • 7
  • 37
  • 56
  • 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.... – Pritesh Apr 04 '12 at 11:21
  • 1
    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. – deadlyvices Apr 04 '12 at 11:35
  • 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). – RemarkLima Apr 04 '12 at 12:01
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.

MCattle
  • 2,897
  • 2
  • 38
  • 54