System::String::Format and Variable parameters 
Author Message
 System::String::Format and Variable parameters

Hello All,
Consider this simple codes:

 System::String __gc* strTmp1 = String::Format(S"{0}, {1}, {2}",
  S"0", S"1", S"2");

 System::String __gc* strTmp2 = String::Format(S"{0}, {1}, {2}, {3}",
  S"0", S"1", S"2", S"3");

strTmp1 will compile but strTmp2 will not because the VC++ does not
allow us to use variable parameters! What then is the use of the
Format method to us VC++ programmers?

Even in C# and VB.NET you can do this. I have to format database
entries format a table of 54 columns and the format statement will not
work. It works perfectly in C# codes for a different project.

Best regards,
Paul.



Sat, 08 Oct 2005 13:44:38 GMT  
 System::String::Format and Variable parameters

Quote:

>  System::String __gc* strTmp1 = String::Format(S"{0}, {1}, {2}",
>   S"0", S"1", S"2");

>  System::String __gc* strTmp2 = String::Format(S"{0}, {1}, {2}, {3}",
>   S"0", S"1", S"2", S"3");

> strTmp1 will compile but strTmp2 will not because the VC++ does not
> allow us to use variable parameters! What then is the use of the
> Format method to us VC++ programmers?

> Even in C# and VB.NET you can do this. I have to format database
> entries format a table of 54 columns and the format statement will not
> work. It works perfectly in C# codes for a different project.

The second case does also NOT WORK in C#!!!

The System.String.Format-Method has only the following overloads (in C#-
syntax):

public static string Format(string, object);
public static string Format(string, object, object);
public static string Format(string, object, object, object);
public static string Format(string, params object[]);
public static string Format(IFormatProvider, string, params object[]);

As you can see, there are 5 methods, three of them are for 1, 2 and 3
parameters. 4 parameters are not supported directly; instead you have to
use the object-Array Method.

C#:
object [] o = {"1", "2", "3", "4"};
string s = string.Format("{0}, {1}, {2}, {3}", o);

Managed C++:
System::Object *o[] = {S"1", S"2", S"3", S"4"};
System::String *s = System::String::Format(S"{0}, {1}, {2}, {3}", o);

--
Greetings
  Jochen

  Do you need a memory-leak finder ?
  http://www.codeproject.com/useritems/leakfinder.asp



Sat, 08 Oct 2005 14:00:25 GMT  
 System::String::Format and Variable parameters
Jochen,

Quote:

> >  System::String __gc* strTmp1 = String::Format(S"{0}, {1}, {2}",
> >   S"0", S"1", S"2");

> >  System::String __gc* strTmp2 = String::Format(S"{0}, {1}, {2}, {3}",
> >   S"0", S"1", S"2", S"3");

> > strTmp1 will compile but strTmp2 will not because the VC++ does not
> > allow us to use variable parameters! What then is the use of the
> > Format method to us VC++ programmers?

> > Even in C# and VB.NET you can do this. I have to format database
> > entries format a table of 54 columns and the format statement will not
> > work. It works perfectly in C# codes for a different project.

> The second case does also NOT WORK in C#!!!

Actually, the second case sure works in C#. The reason for that is that this
overload is used:
public static string Format(string, params object[]);

The C# compiler recognizes "params"  (caused by using the
ParamsArrayAttribute attribute) to mean a method taking "a variable number
of arguments", and so will build the array for you behind the scenes.

As you well point out, though, ou have to do it by hand in MC++.

--
Tomas Restrepo



Sat, 08 Oct 2005 19:04:21 GMT  
 System::String::Format and Variable parameters

Quote:

> Jochen,

> The C# compiler recognizes "params"  (caused by using the
> ParamsArrayAttribute attribute) to mean a method taking "a variable
> number of arguments", and so will build the array for you behind the
> scenes.

Great... I never new that... (of course I never tried it...)

Thanx

--
Greetings
  Jochen

  Do you need a memory-leak finder ?
  http://www.codeproject.com/useritems/leakfinder.asp



Sat, 08 Oct 2005 19:08:21 GMT  
 System::String::Format and Variable parameters

Quote:
>  Subject: Re: System::String::Format and Variable parameters
>  Date: Tue, 22 Apr 2003 06:04:21 -0500
>  Lines: 35
>  X-Priority: 3
>  X-MSMail-Priority: Normal
>  X-Newsreader: Microsoft Outlook Express 6.00.3790.0
>  X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0

>  Newsgroups: microsoft.public.dotnet.languages.vc
>  NNTP-Posting-Host: cable200-75-79-194.epm.net.co 200.75.79.194
>  Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
>  Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:22653
>  X-Tomcat-NG: microsoft.public.dotnet.languages.vc

>  Jochen,


>  > >  System::String __gc* strTmp1 = String::Format(S"{0}, {1}, {2}",
>  > >   S"0", S"1", S"2");

>  > >  System::String __gc* strTmp2 = String::Format(S"{0}, {1}, {2}, {3}",
>  > >   S"0", S"1", S"2", S"3");

>  > > strTmp1 will compile but strTmp2 will not because the VC++ does not
>  > > allow us to use variable parameters! What then is the use of the
>  > > Format method to us VC++ programmers?

>  > > Even in C# and VB.NET you can do this. I have to format database
>  > > entries format a table of 54 columns and the format statement will
not
>  > > work. It works perfectly in C# codes for a different project.

>  > The second case does also NOT WORK in C#!!!

>  Actually, the second case sure works in C#. The reason for that is that
this
>  overload is used:
>  public static string Format(string, params object[]);

>  The C# compiler recognizes "params"  (caused by using the
>  ParamsArrayAttribute attribute) to mean a method taking "a variable
number
>  of arguments", and so will build the array for you behind the scenes.

>  As you well point out, though, ou have to do it by hand in MC++.

>  --
>  Tomas Restrepo


The above code should work:
System::String __gc* strTmp1 = String::Format(S"{0}, {1}, {2}",
   S"0", S"1", S"2");
 System::String __gc* strTmp2 = String::Format(S"{0}, {1}, {2}, {3}",
   S"0", S"1", S"2", S"3");

This was a managed C++ bug. It exists in Everett compiler. We have it fixed
in the next release.

Thanks

--
Liu Xiong, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights.

--------------------



Mon, 17 Oct 2005 02:20:34 GMT  
 System::String::Format and Variable parameters
Hi Liu,

Quote:
> The above code should work:
> System::String __gc* strTmp1 = String::Format(S"{0}, {1}, {2}",
>    S"0", S"1", S"2");
>  System::String __gc* strTmp2 = String::Format(S"{0}, {1}, {2}, {3}",
>    S"0", S"1", S"2", S"3");

The second one most certainly does not compile, not even in everett RTM...

As to whether it is a bug, or not, I think it's debatable, since afaik, the
MC++ language specification is silent on this issue...
--
Tomas Restrepo



Mon, 17 Oct 2005 06:58:10 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. CString::Format execute with variables parameters

2. Format string as a parameter to printf()?

3. System::String/StringBuilder as out parameter

4. Variable as format string

5. Variable String Format Problem

6. printf and variable length string format (asterisk)

7. How can I set string variable to CString variable

8. int to std::string to System::String

9. static std::string exposed through System::String *

10. System::String to std::string

11. System.String to std::string conversion

12. format of COleDateTime recordset parameters

 

 
Powered by phpBB® Forum Software