C++ in C#...

O

odwrotnie

Hi,

i have a situation:

C++ class:
namespace Shape
{
public ref class Shape
{
protected:
String^ name;
String^ color;

public:
Shape(String^ name, String^ color)
{
this->name = name;
this->color = color;
}
String^ toString()
{
String^ s;
s->Format("shape name = %s, shape color = %c", name->ToCharArray(),
color->ToCharArray());
return s;
}
};
}

C#class:
namespace ManyDLLs
{
class Program
{
static void Main(string[] args)
{
Shape.Shape s = new Shape.Shape("ksztalt", "bialy");
Console.WriteLine("s: " + ((Shape.Shape)s).toString());

Console.ReadKey();
}
}
}

But it prints only "s:". :(.

Do you know what I did wrong?
 
W

Willy Denoyette [MVP]

Firts, you never assign to s, so you return a null reference.
Second, Format is a static method, though C++ allows you to call a static
method on an instance reference, you better call String::Format(....).
Third, the format specifiers %s and %c are not valid in C++/CLI, search the
MSDN docs for "Format specifiers", they are exactly the same as for C#.

s = String::Format("shape name = {0}, shape color = {1}",
name->ToCharArray(), color->ToCharArray());

Fourth, ToCharArray returns an array reference, the output of above will
look like this:
s:shape name = System.Char[] ....

I guess this is not what you are after.

Willy.


Hi,

i have a situation:

C++ class:
namespace Shape
{
public ref class Shape
{
protected:
String^ name;
String^ color;

public:
Shape(String^ name, String^ color)
{
this->name = name;
this->color = color;
}
String^ toString()
{
String^ s;
s->Format("shape name = %s, shape color = %c", name->ToCharArray(),
color->ToCharArray());
return s;
}
};
}

C#class:
namespace ManyDLLs
{
class Program
{
static void Main(string[] args)
{
Shape.Shape s = new Shape.Shape("ksztalt", "bialy");
Console.WriteLine("s: " + ((Shape.Shape)s).toString());

Console.ReadKey();
}
}
}

But it prints only "s:". :(.

Do you know what I did wrong?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top