How to link vc++ and c# projects in visual studio? - c#

Is it possible to link C# and Visual C++ projects under 1 solution?
How can I do this?
I hav created a class in c# which contains two methods display() and ListSql().
And how to use these methods of c# project in vc++ project.which type of project i need to
create to do this.
I do like following
I have created a classlibrary which contains a Test class in that Display() and ListSql()
and i have a refence to c# project from vc++ project.
and i have writen as. i didnt write any import statement
using namespace Test;
void main()
{
}
// error namespace Test doesnot exist like wise error

If you are asking whether you can have two projects with different languages in the same solution, then yes. Just right click on the solution and add the project.
From that point you would need to add a reference from one project to the other.
Project -> Add Reference

Related

Which DLL do I have to reference for NetFwTypeLib in a C# project?

I'm dealing with a C# project that states
using NetFwTypeLib;
The C# compiler doesn't recognize it, saying
Type or Namespace name "NetFWTypeLib" was not found.
What do I have to do to add it to my C# project?
I see C++ examples for this, but I don't see which DLL that would be for a C# project.
Thank you!
Right click on the project -> Add reference -> NetFwtypeLib
if it's not working maybe this will be useful

How to use class from other project in visual studio C#

I have a Xamarin.Forms project and a C# console app project. I want to use one class from the console app in my Xamarin.Forms project.
I added the console app project to the solutions explorer of the Xamarin.Forms project.
Unfortunately, I can't figure out how to use the class from the console app in one of the Xamarin files.
I always get the error message:
The name 'MyClass' does not exist in the current context.
I tried to press alt+enter to show potential fixes but it does not offer me the option of importing/using the class.
I also wrote manual using directives in various forms but it still does not seem to make the class accessible.
The only way I was able to use the class was by adding the class directly to the Xamarin project by adding it with add->existing item. The problem with this is that it imports a copy of the class. Since I'm still working on the class within the other project the added class is fast outdated and I have to manually copy its contents over.
How can I use a class from an external project without making a copy of the file?
Instead of access class from console app(its exe) try creating new reusable library add that class and use in both projects also you can write wrapper class in both projects
Try to add a reference to the second project in your first project. To do this, right-click on your project, select Add Reference then select the project in your solution. Once your main project references the second project, then you can access its public types.

How to run C# class library which is added to VB.NET existing solution?

Hi I have console application which is written in VB.NET. For this I added c# class library with existing solution. I wrote code in c# class library. Now when I try to run, it is going to VB.NET console application by default. I am not able to run my c# class library.
For clear understanding lets call VB.NET project as classVB and class library as classC#
I tired these methods to make it work :
1) Added classC# reference to my classVB project and made classVB as startup project. I used using statement also to refer to my classVB project as Using classVB. And I put break point in my classC#.But still it is pointing to classVB project
2) Tried to made classC# as startup project( Even I knew this doesn't gonna work). For this I am getting usual error which says "A project with an output Type of class library cannot be started directly."
3) Right click on solution and start up project option -> Single startup project and selected classVB.
But none of this is working. It is always pointing to my classVB project even after I put breakpoint on c#class library code.
Its the first time I am working on library class, so any help on this would be appreciated.
You can't directly run a class library. It is not executable. You can reference the code from it in your VB project, but a class library can never run by itself. To access a public method in your class library from VB, refer to it by Namespace.ClassName.MethodName.
Change the compilation output of the library to the same path where you added the reference in the VB.NET application. Generate the library and check the build is successful.
Do not forget to change the library class to configuration debug in the solution to generate the PDB file that will allow you debugging. Put a breakpoint and try again.

I can't reference my class library

I have a solution that contains two projects developed in visual studio 2012 express, and both targeting the .net framwork 4.5.
the first "Dao" project purpose is to take data from a database. and take these data to the second project as a dll library
the second project "UI" purpose is to display data coming from dll library
when i added reference to the second project and wrote using statement, I got the following error:
The type or namespace name 'Dao' could not be found (a using directive or an assembly reference missing?)
I tried to change the target of the two projects to .net framework 4.0 and .net framework 3.5 , but I got the same error.
I also add this piece of code to be sure that the target is change but I got true :
using System;
using Dao; // error
namespace Ui
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Type.GetType("System.Reflection.ReflectionContext", false) != null);
Console.ReadKey();
}
}
}
What do I need to do to fix the problem? Thanks a bunch.
You need to add a reference to your Dao assembly from your UI assembly. Right Click on References, Add Reference. In the Projects tab, select your Dao project and hit OK.
First things first:
Add a reference to Dao - in source explorer right click references->Add->projects tab.
Add a using statement at the top of your code something like using Dao;
Ensure Dao is a public class
This way your code will know to reference Dao, it is usually better to create a new instance of Dao:
Dao example = new Dao();
Then when calling Dao you would call example instead, so example.(name of method)

How do I compile C# code as a library instead of an executable?

I have a C# console application in Visual Studio 2010. It has a Main() method as well as a bunch of utility classes. I'd like those utility classes to be available to other solutions. From reading online it seems that I need to compile it as a Class Library (DLL). So here's what I did:
Went in Visual Studio to "Project > [ProjectName] Properties > Application" and changed "Output type" from "Console Application" to "Class Library"
Rebuilt; ProjectName.dll was created in bin/Debug.
Created a new Console Application
Solution Explorer > Add Reference > browse to ProjectName.DLL, select it.
However, neither IntelliSense nor the Object Browser could find the classes inside that DLL.
I tried recompiling several different Console Applications as Class Libraries and got the same result. I also noticed that it works if I initially create the solution as a Class Library, but not if I convert it to one later.
Any tips?
You do not need to build it as a dll. VS 2010 (and IIRC 2008) allow referencing exe assemblies. All you need is for they relevant types to be declared public - top-level classes defualt to internal if you don't add a specifier.
You can switch output type to Class library in project properties as well - then you will have an output as dll instead exe file
What I've always done (since this is what you do with C++ static libraries, which is what I normally use - though I think it has some advantages for C# too) is add the class library's project to the solution, then add a reference to it in the project (or projects) that uses it.
When you go to add a reference, the list of potential references includes items from the solution, so it should be fairly obvious what to do. You should then get intellisense for your library.
One advantage of doing things this way is that if you need to edit files in the library project, it's very straightforward because they are close to hand, and the project then gets rebuilt automatically when you compile the solution.
Make sure that the classes in your dll project are public.
At first, from the point of view of managed libraries it does not matter what kind of Output type is your managed library. I mean that you can successfully reference ConsoleApplication1.exe from ConsoleApplication2.exe project (so you have no reason to convert ConsoleApplication1.exe to ConsoleApplication1.dll).
At second, I've tried to reproduce your situation, but... without effect. My VS displays types/methods from ConsoleApplication1.dll. One reason I can suppose is that you have forgotten to set visibility modifier (public keyword) for your utility classes.

Categories

Resources