Mixed languages in Visual Studio  
Author Message
prog.gabi





PostPosted: Visual C# General, Mixed languages in Visual Studio Top

Can I use mixed languages in Visual Studio i.e. some source files in C# (.cs), others in C++ (.cpp)

Visual C#19  
 
 
ahmedilyas





PostPosted: Visual C# General, Mixed languages in Visual Studio Top

you can add several various .NET language projects in 1 solution yes, but in terms of C++ I'm not sure and don't quite think so, but do not quote me on this. if its a VC++.NET then perhaps so yes but either way you need a new project to include those supported language files in the project

 
 
prog.gabi





PostPosted: Visual C# General, Mixed languages in Visual Studio Top

Thank you for such a prompt reply. Yes, the C++ I have in mind is VC++.NET, so it's worth a try. But I need to know how to call the C# project from the C++ project. Do I declare the C# function as "external" in the C++
 
 
Peter Ritchie





PostPosted: Visual C# General, Mixed languages in Visual Studio Top

You can have any number supported language projects in a solution. Each project is limited to one language, you can't mix languages in one project (other than C and C++ in a C++ project)

 
 
prog.gabi





PostPosted: Visual C# General, Mixed languages in Visual Studio Top

I am grateful to both respondents but, alas, I still don't know how to do it! As a simple exercise I tried to build a solution with two projects, both in C#. I would like one of these to run a simple calling program, the other to contain a simple subroutine that is called. I may be dumb but I have not succeeded in this. Please help!
 
 
ahmedilyas





PostPosted: Visual C# General, Mixed languages in Visual Studio Top

I would have thought you needed to add a reference to the calling project in your caller project. Then access it via the namespace.classname approach. Example if I had a VB.NET project and C# project, I wanted to access VB.NET Class from C#, I would add a reference in my C# project to the VB.NET project, then in C# I would import the namespace of the VB.NET project, then call it....

MyVBClass theClass = new MyVBClass();

theClass.DoShowHello(); //example



 
 
prog.gabi





PostPosted: Visual C# General, Mixed languages in Visual Studio Top

Please excuse my stupidity! I do not understand your two lines of code and we will, I think, save time if I tell you what I did which does not work. In this case, both of my projects are in C# because it is the language I know best and did not want to get stuck with syntax errors. I have a solution called "ForumApplication" whose two projects are "ForumApplication" and "ForumApplication2". Each has only one source file called, respectively, "Program.cs" and "Program2.cs". Here they are:

//This is Program.cs in project ForumApplication which contains a reference

//to project ForumApplication:

using System;

using System.Collections.Generic;

using System.Text;

namespace ForumApplication

{

class Program

{

static void Main(string[] args)

{

Program2.DoShowHello();

}

}

}

------------------------------------------------------------------

//This is Program2.cs in project ForumApplication2 which is referenced

//in project ForumApplication

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

namespace ForumApplication

{

class Program2

{

static void DoShowHello()

{

Console.Write("Hello, World!");

return;

}

}

}

----------------------------------------------------------

The answer must be obvious but I just don't see it. Thanks a million.


 
 
ahmedilyas





PostPosted: Visual C# General, Mixed languages in Visual Studio Top

my code was just a simple example my friend! It was just a made up class and made up method demonstrating on how to call a class/method from a different language to a different language

 
 
AlexBB





PostPosted: Visual C# General, Mixed languages in Visual Studio Top

Please excuse my stupidity! I do not understand your two lines of code and we will, I think, save time if I tell you what I did which does not work. In this case, both of my projects are in C# because it is the language I know best and did not want to get stuck with syntax errors. I have a solution called "ForumApplication" whose two projects are "ForumApplication" and "ForumApplication2". Each has only one source file called, respectively, "Program.cs" and "Program2.cs". Here they are:

//This is Program.cs in project ForumApplication which contains a reference

----------------------------------------------------------

The answer must be obvious but I just don't see it. Thanks a million.

I guess what Ahmadillyas is trying to say is that you should have an explicit reference to namespace for the second project in your first one. I tried to edit your code this way. I want to caution you that I am not an expert by any means, that I am going to be facing a similar problem next week and will follow this discussion closely. However I have seen such code samples, and one of them actually works in my project. The difference is only that the two namespaces in my case are parts of the same (single) project. Here it goes.

//This is Program.cs in project FormApplicationONE which contains a reference
//to project FormApplicationTWO:
using System;
using System.Collections.Generic;
using System.Text;
using FormApplicationTWO;

namespace FormApplicationONE
{
class Program
{
static void Main(string[] args)
{
Program2.DoShowHello();
}
}
}

------------------------------------------------------------------
//This is Program2.cs in project FormApplicationTWO which is referenced
//in project FormApplicationONE
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace FormApplicationTWO
{
class Program2
{
static void DoShowHello()
{
Console.Write("Hello, World!");
return;
}
}
}



 
 
prog.gabi





PostPosted: Visual C# General, Mixed languages in Visual Studio Top

Dear Alex, thank you for trying. Unfortunately your modification still does not work. I think the crux must be in AhmadIlyas's line

MyVBClass theClass = new MyVBClass();

which I have not been able to interpret in terms of my variables. As it is, the compiles cannot find the required namespace; also it complains because there is no entry point in Program2.


 
 
Peter Ritchie





PostPosted: Visual C# General, Mixed languages in Visual Studio Top

Example:

Create new solution with Visual C# WinForms application in Visual Studio:

  • File/New Project
  • If your default language is C#, select Visual C#/Windows from the Project Types tree and skip the next step
  • If you default language is not C#, select Other Languages/Visual C#/Windows from the Project Types tree.
  • Select "Windows Application" from the Templates view.
  • Enter a name for your application.
  • Ensure "Create directory for solution" is checked.
  • If needed, change the Solution name to something more appropriate.
  • Press OK

To create a project with another language for use in the previous project:

  • Right click the solution in the Solution Explorer
  • Select Add\New Project
  • If your default language is C++, select Visual C++/CLR from the Project Types tree and skip the next step
  • If you default language is not C++, select Other Languages/Visual C++/CLR from the Project Types tree.
  • Select "Class Library" from the Templates view.
  • Enter the name of your project (for this example "ClassLibrary")
  • Press OK.
  • By default the new project should contain a empty "Class1" class. Add a method to the class, e.g:

    public: int GetNumber()

    {

    return 42;

    }

To use the ClassLibrary project from the first project you need to add it as a reference:

  • Expand the application project in the Solution Explorer.
  • Right click "References"
  • Select "Add Reference"
  • Click the Projects Tab
  • Double click the project you require (in this example "ClassLibrary").

You now have a reference to that class library. To use the method we just described you need to create an instance of Class1 and make the call. This can be done as follows:

ClassLibrary.Class1 object1 = new ClassLibrary.Class1();

int value = object1.GetNumber();

If you don't want to have to use the class libraries namespace name ("ClassLibrary", in this example), whenever you reference a type from that class library you can add a using directive near the top of your file. In this example:

using ClassLibrary;

Which would then allow to write the following code instead:

Class1 object1 = new Class1();

int value = object1.GetNumber();

Compile and run. You're now using C++ code from a C# application.



 
 
prog.gabi





PostPosted: Visual C# General, Mixed languages in Visual Studio Top

A thousand thanks. Following your directions to the letter I got a program that works, which is more than I can say for any of my prior attempts. I can now get back to work!
 
 
prog.gabi





PostPosted: Visual C# General, Mixed languages in Visual Studio Top

I want to reopen this because I need to do the reverse -- access a C# program from a C++ program. By and large I follow the same sequence so wonderfully explained by Peter Ritchie, opening a C# class library and putting a reference to it in my main C++ program. I puzzled a little about how to say the equivalent of

ClassLibrary.Class1 object1 = new ClassLibrary.Class1();

in C++, but found that I didn't need to: just the line

using namespace ClassLibrary;

together with the reference, seemed to do the trick. However, there are some C++ programs that won't let me do this, and the compiler continues to complain that no such namespace exists. One of them is the one I am interested in: it is the C++ version of "CaptureSound" in the DirectX Sample Browser. I think it has something to do with its being a multithread program. Can anybody help