Wrox Programmer Forums
|
BOOK: Beginning Visual C#
This is the forum to discuss the Wrox book Beginning Visual C#, Revised Edition of Beginning C# for .NET v1.0 by Karli Watson, David Espinosa, Zach Greenvoss, Jacob Hammer Pedersen, Christian Nagel, Jon D. Reid, Matthew Reynolds, Morgan Skinner, Eric White; ISBN: 9780764543821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual 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 June 3rd, 2003, 08:40 PM
Registered User
 
Join Date: Jun 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default string format

Hello... this is my first post in any coding forums. This is a great idea. Newbie here.

In a console application, it's common to write a string as follows:
string str = "The name of the author is {0}", vName;

Using the {0} syntax does not seem to work in a windows form when assigning a string to a label.txt , for instance.

Is there any way to use this syntax in a windows form?

mark
 
Old June 3rd, 2003, 09:39 PM
Registered User
 
Join Date: Jun 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to shogunofharlem
Default

Mark,

In a console app, it's common to writeout a string with the following syntax:
Console.WriteLine("The name of the author is {0}", vName);

string str = "The name of the author is {0}", vName will not work.

Here is the code that you would need to do that in a windows form.
string str = "The author name is {0}";

StringBuilder sb = new StringBuilder();

sb.AppendFormat(str,vName);
label1.Text = sb.ToString();

Hope that works for you.

Shogunofharlem
 
Old June 3rd, 2003, 09:50 PM
Registered User
 
Join Date: Jun 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

shogunofharlem...

thanks a bunch

mark
 
Old June 4th, 2003, 01:19 AM
Registered User
 
Join Date: Jun 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

you can also get a formated string this way:

string myFormatedString = string.Format("The Name: {0}", vName);


4log
 
Old June 4th, 2003, 07:32 AM
Registered User
 
Join Date: Jun 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

bonus! Thanks 4log!
 
Old June 4th, 2003, 08:42 AM
Registered User
 
Join Date: Jun 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to shogunofharlem
Default

Mark,

You are welcome. 4log is correct as well. I was coming back to post the same thing, then saw his post. It just proves that there are more then one way to do things.:D
 
Old June 23rd, 2003, 07:42 AM
G2k G2k is offline
Registered User
 
Join Date: Jun 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to G2k
Default

I have a question: why is C#'s way of writing a variable to a console for example more complicated than what it could be? You use the {0} {1} and so on and then you have to define those variables like var1, var2 ....but that just makes no sense. Why does one have to write more code? I come from a Javascript background so I'm more used to seeing directly the var1 and var2. Wouldnt that make everything easier? Please correct me if I'm wrong.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with String Format(ASP.NET C#) richie86 ASP.NET 1.0 and 1.1 Basics 3 September 20th, 2007 07:43 PM
format-number on a string? dep XSLT 1 October 31st, 2006 01:24 PM
Input string was not in a correct format. palleti VB.NET 2002/2003 Basics 2 October 17th, 2006 10:38 AM
String format gugu C# 1 April 20th, 2006 03:08 PM





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