By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,783 Members | 1,347 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,783 IT Pros & Developers. It's quick & easy.

C++ in C#...

P: n/a
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?

--
Best regards,
Odwrotnie.
May 12 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.
"odwrotnie" <kr*****@gazeta.pl> wrote in message
news:op.s9ge2g0rdvqlmf@celsius...
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?

--
Best regards,
Odwrotnie.
May 12 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.