Discussion:
Problem with String::Format
(too old to reply)
MT
2003-12-12 00:34:03 UTC
Permalink
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
Bart Jacobs
2003-12-12 09:28:37 UTC
Permalink
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
Post by MT
Does anybody know if String::Format is messed up in C++. Here's a line of
Console::WriteLine(String::Format(new
System::Globalization::CultureInfo("en-US"), "{0} : {1} %", S"Haha",
__box(12.00)));
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
2003-12-12 14:51:11 UTC
Permalink
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
Post by Bart Jacobs
MT,
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
Post by MT
Does anybody know if String::Format is messed up in C++. Here's a line of
Console::WriteLine(String::Format(new
System::Globalization::CultureInfo("en-US"), "{0} : {1} %", S"Haha",
__box(12.00)));
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
2003-12-12 15:01:41 UTC
Permalink
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
Post by MT
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
Post by Bart Jacobs
MT,
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
Post by MT
Does anybody know if String::Format is messed up in C++. Here's a line
of
Post by Bart Jacobs
Post by MT
Console::WriteLine(String::Format(new
System::Globalization::CultureInfo("en-US"), "{0} : {1} %", S"Haha",
__box(12.00)));
error C2665: 'System::String::Format' : none of the 5 overloads can
convert
Post by Bart Jacobs
Post by MT
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
Carl Daniel [VC++ MVP]
2003-12-12 15:30:15 UTC
Permalink
Post by MT
__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

Loading...