Problem with String::Format

  • Thread starter Thread starter MT
  • Start date Start date
M

MT

Does anybody know if String::Format is messed up in C++. Here's a line of
code:
Console::WriteLine(String::Format(new
System::Globalization::CultureInfo("en-US"), "{0} : {1} %", S"Haha",
__box(12.00)));

Here's the error I get:

error C2665: 'System::String::Format' : none of the 5 overloads can convert
parameter 1 from type 'System::Globalization::CultureInfo __gc *'


All I am trying to do is force US English locale with String::Format. Is
this the wrong way of doing things?

Thanks,

MT
 
MT,

The right way to call this overload of String::Format is as follows:

Object *args[] = {S"Haha", __box(12.00)};
CultureInfo *cultureInfo = new CultureInfo("en-US");
String *s = String::Format(cultureInfo, "{0} : {1} %", args);
Console::WriteLine(s);

You have to create an array explicitly because Managed Extensions does
not support .NET-style variable-length argument lists. It ignores the
ParamArray attribute on the "args" parameter of String::Format. (C# and
VB.NET do support this.) Make sure to choose the C++ language filter in
the MSDN Library.

Bart Jacobs
 
That's very strange because this works:
Console::WriteLine(String::Format("{0} : {1} %", S"Haha", __box(12.00)));

It only gives me an error when I put the culture info in....

MT


Bart Jacobs said:
MT,

The right way to call this overload of String::Format is as follows:

Object *args[] = {S"Haha", __box(12.00)};
CultureInfo *cultureInfo = new CultureInfo("en-US");
String *s = String::Format(cultureInfo, "{0} : {1} %", args);
Console::WriteLine(s);

You have to create an array explicitly because Managed Extensions does
not support .NET-style variable-length argument lists. It ignores the
ParamArray attribute on the "args" parameter of String::Format. (C# and
VB.NET do support this.) Make sure to choose the C++ language filter in
the MSDN Library.

Bart Jacobs
Does anybody know if String::Format is messed up in C++. Here's a line of
code:
Console::WriteLine(String::Format(new
System::Globalization::CultureInfo("en-US"), "{0} : {1} %", S"Haha",
__box(12.00)));

Here's the error I get:

error C2665: 'System::String::Format' : none of the 5 overloads can convert
parameter 1 from type 'System::Globalization::CultureInfo __gc *'


All I am trying to do is force US English locale with String::Format. Is
this the wrong way of doing things?

Thanks,

MT
 
In fact, MSDN documentation has the following example:
__property String* get_numOfAccesses()
{
return String::Format(S"The identity of {0} has been accessed {1}
times.",
_principal->Identity->Name,
__box(_nAccesses));
}

which contradicts with your statement that C++ does not support .NET-style
variable-length argument lists. Who is correct?

MT

MT said:
That's very strange because this works:
Console::WriteLine(String::Format("{0} : {1} %", S"Haha", __box(12.00)));

It only gives me an error when I put the culture info in....

MT


Bart Jacobs said:
MT,

The right way to call this overload of String::Format is as follows:

Object *args[] = {S"Haha", __box(12.00)};
CultureInfo *cultureInfo = new CultureInfo("en-US");
String *s = String::Format(cultureInfo, "{0} : {1} %", args);
Console::WriteLine(s);

You have to create an array explicitly because Managed Extensions does
not support .NET-style variable-length argument lists. It ignores the
ParamArray attribute on the "args" parameter of String::Format. (C# and
VB.NET do support this.) Make sure to choose the C++ language filter in
the MSDN Library.

Bart Jacobs
Does anybody know if String::Format is messed up in C++. Here's a line of
code:
Console::WriteLine(String::Format(new
System::Globalization::CultureInfo("en-US"), "{0} : {1} %", S"Haha",
__box(12.00)));

Here's the error I get:

error C2665: 'System::String::Format' : none of the 5 overloads can convert
parameter 1 from type 'System::Globalization::CultureInfo __gc *'


All I am trying to do is force US English locale with String::Format. Is
this the wrong way of doing things?

Thanks,

MT
 
MT said:
In fact, MSDN documentation has the following example:
__property String* get_numOfAccesses()
{
return String::Format(S"The identity of {0} has been accessed
{1} times.",
_principal->Identity->Name,
__box(_nAccesses));
}

which contradicts with your statement that C++ does not support
.NET-style variable-length argument lists. Who is correct?

Both are. There are explicit overloads of String::Format taking 0, 1 and 2
(I think) additional arguments past the format, plus the overload taking
Object[].

In the Whidbey ("VC8") release, VC++ is expected to support ".NET style
varargs" 100%.

-cd
 
Back
Top