Click here to Skip to main content
15,899,475 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
QuestionImplicit type casts, is this possible Pin
mike toon28-Oct-08 5:04
mike toon28-Oct-08 5:04 
AnswerRe: Implicit type casts, is this possible Pin
Mark Salsbery28-Oct-08 6:33
Mark Salsbery28-Oct-08 6:33 
GeneralRe: Implicit type casts, is this possible [modified] Pin
mike toon29-Oct-08 1:36
mike toon29-Oct-08 1:36 
GeneralRe: Implicit type casts, is this possible Pin
Mark Salsbery29-Oct-08 5:22
Mark Salsbery29-Oct-08 5:22 
GeneralRe: Implicit type casts, is this possible Pin
mike toon30-Oct-08 0:52
mike toon30-Oct-08 0:52 
GeneralRe: Implicit type casts, is this possible Pin
mike toon30-Oct-08 6:03
mike toon30-Oct-08 6:03 
GeneralRe: Implicit type casts, is this possible Pin
led mike30-Oct-08 6:13
led mike30-Oct-08 6:13 
GeneralRe: Implicit type casts, is this possible [modified] Pin
Mark Salsbery30-Oct-08 9:08
Mark Salsbery30-Oct-08 9:08 
mike toon wrote:
I had been compiling a dll library in managed c++ which was to be used by c# code.


In that case you should be using the second version of my sample -
the managed version (as also shown in led mike's link).

Like I mentioned, the operators in that form are implicit unless explicitly set
explicit with the keyword explicit. Big Grin | :-D
// In C++ DLL

using namespace System;

namespace TestCPlusPlusClassLibrary 
{

public ref class numericstring
{
    String ^str;
public:
    numericstring()
    {
        str = gcnew String("123.456");
    }

    static explicit operator bool(numericstring^ val) {return Convert::ToDouble(val->str) != 0;}
    static explicit operator int(numericstring^ val) {return Convert::ToInt32(Convert::ToDouble(val->str));}
    static explicit operator double(numericstring^ val) {return Convert::ToDouble(val->str);}
};

}

// In C#

...
    TestCPlusPlusClassLibrary.numericstring ns = new TestCPlusPlusClassLibrary.numericstring();
    bool nsbool = ns;
    int nsint = ns;
    double nsdouble = ns;
...

Note how WITH the "explicit" keyword in the example above, the C# will not compile.
Without the "explicit" keyword, it compiles and runs fine.

Mark

Mark Salsbery
Microsoft MVP - Visual C++

Java | [Coffee]

modified on Thursday, October 30, 2008 3:20 PM

GeneralRe: Implicit type casts, is this possible Pin
mike toon30-Oct-08 23:52
mike toon30-Oct-08 23:52 
QuestionI need help 1 items on the program administrators: (run on DOS) Pin
qt_oct26-Oct-08 19:22
qt_oct26-Oct-08 19:22 
AnswerRe: I need help 1 items on the program administrators: (run on DOS) Pin
Paul Conrad27-Oct-08 4:55
professionalPaul Conrad27-Oct-08 4:55 
AnswerCP IGNORE: homework Pin
leckey27-Oct-08 10:05
leckey27-Oct-08 10:05 
QuestionIncrementally convert existing native MFC Application to .NET Pin
Rolf Kristensen24-Oct-08 14:16
Rolf Kristensen24-Oct-08 14:16 
AnswerRe: Incrementally convert existing native MFC Application to .NET Pin
StevenS_Dev28-Oct-08 4:35
StevenS_Dev28-Oct-08 4:35 
GeneralRe: Incrementally convert existing native MFC Application to .NET [modified] Pin
Rolf Kristensen28-Oct-08 5:10
Rolf Kristensen28-Oct-08 5:10 
Questionconst char* to char* conversion Pin
auralius manurung23-Oct-08 20:10
auralius manurung23-Oct-08 20:10 
AnswerRe: const char* to char* conversion Pin
Mark Salsbery23-Oct-08 20:23
Mark Salsbery23-Oct-08 20:23 
AnswerRe: const char* to char* conversion Pin
KarstenK23-Oct-08 21:10
mveKarstenK23-Oct-08 21:10 
GeneralRe: const char* to char* conversion Pin
auralius manurung23-Oct-08 22:24
auralius manurung23-Oct-08 22:24 
Questioncreate a property with class type (class name is : system::io::ports:serialport) Pin
iman_kh21-Oct-08 22:05
iman_kh21-Oct-08 22:05 
QuestionRe: create a property with class type (class name is : system::io::ports:serialport) Pin
Mark Salsbery22-Oct-08 5:50
Mark Salsbery22-Oct-08 5:50 
AnswerRe: create a property with class type (class name is : system::io::ports:serialport) Pin
N a v a n e e t h22-Oct-08 18:15
N a v a n e e t h22-Oct-08 18:15 
GeneralRe: create a property with class type (class name is : system::io::ports:serialport) Pin
iman_kh23-Oct-08 8:33
iman_kh23-Oct-08 8:33 
QuestionSystem Date Pin
mikobi20-Oct-08 19:20
mikobi20-Oct-08 19:20 
AnswerRe: System Date Pin
Giorgi Dalakishvili20-Oct-08 20:22
mentorGiorgi Dalakishvili20-Oct-08 20:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.