Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 27th, 2007, 09:22 PM
Registered User
 
Join Date: Nov 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default return statements with strings

So here is the issue I have the book Professional C# 2005, (This is not a question on the book itself but rather I use an example to explain) and as I go through it I am keep on seeing some syntax that I don't full understand:

Code:
return string.Format("${0}.{1,-2:00}", Dollars,Cents)
now what I am confused about is how the ${0}.{1,-2:00} works and what it does?



 
Old December 28th, 2007, 03:58 AM
Authorized User
 
Join Date: Sep 2007
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well it is really simple, like printf in old C. Let's forget that ,-2:00 for a second...

{0} is replaced by the second parameter of String.Format (the first one is the format string)
{1} is replaced by the third parameter
... and so on

So let's say Dollars = 11 and Cents = 15, the return value would be "$11.15" String.Format has replaced those {n} placeholders (I'm not sure if placeholder is a correct term here) with actual values.

Now back to that ,-2:00 part in the second placeholder. It is formatting rule. How that second value should be formatted into the string. I just don't know what that -2:00 excatly does and I don't have time to check it out but you can check it from MSDN: http://msdn2.microsoft.com/en-us/library/fht0f5be.aspx

 
Old December 28th, 2007, 04:36 AM
Authorized User
 
Join Date: Apr 2007
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In this statement
return string.Format("${0}.{1,-2:00}", Dollars,Cents)

{0} and {1} are placeholders which are replaced by the variables Dollars and Cents respectively.
And -2:00, formats the second placeholder to two decimal places .... ie, if u have a number say 5 and want to display it as 5.00,then u give the placeholder value say,{1:0.00}.
And -2 in -2:00 is indentation of the decimal points.
As far as i remember, -2 is indentation ... it means that 2 white spaces will be added to the right.If u give 20:00 it adds 20 white spaces to the left.Just check with it.
I hope u've atleast some idea. :)

Gunjan
 
Old December 28th, 2007, 08:49 PM
Registered User
 
Join Date: Nov 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

so why not just write soething like this... Preformance cant be the reason! so what do into all that?

return string.Format("$" + Dollars + "." + Cents);

 
Old December 29th, 2007, 01:21 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Using string.Format in this case doesn't add anything.

By doing only simple concatenation you don't get the output that you get with the use of string.Format().

Here are some examples of inputs and outputs using the two methods:
Code:
Inputs   Concat   Format()
--------------------------
5 24     $5.24    $5.24
0 2      $0.2     $0.02
String.Format() provides the formatting for each input value in the place holders in the string. In this case it is padding the cents value to 2 decimal places. In the case of simple concatenation, the above result could actually be interpreted wrong (20 cents instead of 02 cents).

There are measurable performance gains with the Format method over standard concatenation when you get into lots of string manipulation. But even for simple stuff it can be considerably easier to read and is a good habit to get into. When you build strings that contain quotes it becomes much easier to read. Consider the following examples:

   phrase = "I see";
   wife = "Gertrude";

   message = "\"" + phrase + "\", said the blind man to his deaf wife " + wife + ".";
      //vs.
   message = string.Format("\"{0}\", said the blind man to his deaf wife {1}.", phrase, wife);

The result is the same, and yes there is slightly more code using the Format() method. However, there is only 1 string literal instead of 3 (performance) and from my point of view it's easier to read the second to see the structure of the file string. As construction complexity grows, using the Format() method actually simplifies because you don't have to add more concatenation strings around variables. You just add a replacement token in the right place and another argument to the method call.

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
if statements brainchild Javascript 2 March 1st, 2007 06:08 AM
Case Statements cfriedberg SQL Server 2000 1 September 7th, 2005 08:46 PM
IF ELSE statements gmpurple Classic ASP Databases 4 November 15th, 2004 04:30 PM
C# Conditional statements sporkman43 General .NET 2 July 23rd, 2004 09:10 AM
If, else if statements Joel JSP Basics 0 March 18th, 2004 06:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.