Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use C# Class in VB project in VS? 1

Status
Not open for further replies.

Dinobrago

Technical User
Dec 8, 2001
184
US
In Visual Studio .NET, how can you use a VB class in a C# project & vice versa? Isn't there a way to create a namespace or assembly or solution that I can reference from another project, independent from language?

Any demonstration with simple classes in Visual Studio would be extremely helpful.

Thanks!
Dean
---
goddette@san.rr.com
 
It is quite simple to use a class from VB to C# or viceversa. You create a project, define your namespace and your classes. Then, in the project you want to use these classes, you simply add a reference to the first project and use:

using project1_namespace;

After that, you can use the classes from the other project like they were written in the same language.
So, in brief, no matter in what .NET compliant language you write your assembly (probably a class library) you can use it in another .NET compliant language.
Things get more difficult when you are trying to use things from non-.NET compliant languages. But you can surely do this also. I think that I answered your question, though.
If you have any other problems, just ask!
 
Thanks! After a bit of exploring, I finally found the "Browse" button on the "Projects" tab of the "Add Reference" dialog box.

Dean
---
goddette@san.rr.com
 
Apparently, there are some subtleties here that I don't understand. I have been able to accomplish this with trivial classes but I am running into the following with the actual classes I am working with.

The class works when it is included direclty in a project. I create a second project of type ClassLibrary with that class and built it successfully.

From a new project, Console1, I create a reference to the ClassLibrary and I build the code successfully. But when I run it, I get an error "An unhandled exception of type System.IO.FileNotFoundException occurred in Unknown Module. Additional information: File or assembly name ClassLibrary1, or one of its dependencies, was not found."

I get this same error even if I have the ClassLibrary1 project loaded into my Console1 solution.

Any suggestions?



Dean
---
goddette@san.rr.com
 
Well, I don't know what the problem was exactly but I fixed it. I went back to my simple class library that worked and started reconstructing my classes in THAT ClassLibrary to find the offending code and, Voila, everything works. So apparently I did something to the orignal class library that bolluxed things up, just don't know what.



Dean
---
goddette@san.rr.com
 
When you set a reference to another assembly, VS will copy the DLL into your project's directory. If the other project is recompiled, your using project doesn't know about it.

A better way to do referencing is to set a reference to the other *project*.

Project Explorer | References | Add Reference | Projects | Browse

That way when the other assembly (project) gets recompiled, VS will recognize it and automatically re-copy the new DLL into your project tree.

Chip H.
 
> Useful tip chiph! Here, have a muffin. <

Yum! Banana bread with walnuts! My favorite!

;-)

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top