1

I have a C# class (a lot of lines). I want to use this class in my VB.NET project. So, I tried to add the .cs file to my VB.NET project and then call it by:

MyclassName.VoidName()

It's not working.

('MycalssName' is not declared. It may be inaccessible due to its protection level.)

How can I use a C# class in my VB.NET project?

2
  • 3
    Add a reference to the C# project in your VB.NET project. Then you can access the classes via Namespace.ClassName. Commented Jan 30, 2013 at 22:41
  • If the c# code is compilable, Tim Schmelter's comment is your answer. Commented Jan 30, 2013 at 22:42

2 Answers 2

9

You can simply compile C# code into an assembly (DLL) and reference that assembly from your VB.NET project.

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

Comments

0

A single .NET assembly/project should contain code written in one supported language only. Compilation of any .NET project will need the language specific MSBuild targets (which are included as part of the project files). So, you will be unable to compile/use a C# language source file in a VB.net project and vice versa.

Ignoring the MSBUild aspects, the compilation of a vb.net project (with a c# source file) will not even suceed, as the syntax will be different and compiler (vbc.exe) will throw a lot of compiler errors.

So, its best to compile the C# project and refer to the types from the VB.net project.

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.