12

I wanna use the C# code file in VB.Net project Which is windows based application. But that C# class is not using in VB.NET application. How Can I perform this task.

Thanks

2
  • I don't quite understand your question. Do you want to include the C# file as a resource (like you would do with an image or XML file for example)? Commented Feb 8, 2010 at 9:13
  • I have a C# file as Data Access Layer and I wanna use that in my vb.net windows application. I know how to use different languages classes in asp.net. But I am unable to use in windows applicaton. Commented Feb 8, 2010 at 9:24

4 Answers 4

14

It is very simple to combine your VB.net & C#.net project.

Step1- Add projects which you wanna combine to a single solution.

Step2- Within any one project VB/C# in which you want to call classes from other language project "Goto- Add Reference- Projects" and select the other project which will be automatically displayed.

Step3- After adding the reference just add "Imports (in VB)" or "using (in C#)" statements to your code.

Step4- bingooo!!! now you can use your VB/C# classes in another language.

(tip: You can go only in one direction here i.e. either you can use your C# classes in VB.net or vice versa.)

All the best

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

1 Comment

I'll only clarify on steps 1 & 2 - First add C# project to your VB solution (via solution explorer) then from the VB project's properties, add a reference to the project. You can then access the class public methods directly or via a namespace if any (and they'll be compiled into the code!)
12

Compile the C# class in it's own C# class library (DLL) and then in your VB project, add a reference to the C# DLL. You can then use the class in your VB project.

If, however, you want to include the source code of the C# class in your VB project then you will have to convert the class from C# to VB. There are various methods of doing this, such as the online tool Convert C# to VB.NET

6 Comments

I don't want to make library because that is a single class.
@Deepak - I have added the second paragraph in response to your comment
Thanks, I have converted that code and it is also running but As well as we can do in ASP.NET so I want to use this like web application reference.
@Deepak - sorry I don't understand the last part of your comment
I meant to say as we get reference of different language class file in asp.net to put it in APP_code's sub directory and do configuration in web.config's compilation tag. AS like can we do in windows application by any technique?
|
7

You can also add the reference for an executable, if it is a .NET assembly. So just compile your C# project and add it as reference into your VB project.

2 Comments

Thanks,I know this thing. But As we use vb.net individual class in C# project of ASP.NET. Then we put it in APP_Code's Child Directory and Specify it in web.config's compilation->codeSubDirectories tag. Same I wanna use single class of C# to vb.net windows application.
Well then I don't think that you can do it, except for web projects. I think you have to reference some kind of assembly. How about adding a new C# project into your solution and just add your existing .cs file, compile it as Dll and add the reference.
1

It is possible, check this: http://bytes.com/topic/net/answers/49259-mixing-vb-net-c-same-project

However, it is possible to use different languages in a single project. You may need to write command line build file to build the project. In .NET framework SDK, there is one sample on it. You could access it in C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Samples\Technologies\CrossDevLan guage.

This sample demonstrates the use different development languages in a single project. This sample creates two assemblies. The first is a library or DLL assembly that defines a simple base class written in managed extensions for C++. The second assembly is an executable assembly that defines three derived classes written in C#, VB, and IL (Intermediate Language). These types derive from each other and ultimately from the base class written in managed C++. Finally, the executable creates instances of each of the derived types and calls a virtual method for each. The .NET Framework is an environment where various developers can work together seamlessly while developing in their language of choice.

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.