473,385 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Incorrect string format??

I'm trying to build a string for eventual output to a file. Input
comes from a number of input boxes but I've only test on one text box
content when I get an exception at runtime. The program segment is:

string work;

work = string.Format("//------- {0} ------\r\n", txtPlanet.Text);
work += string.Format("sphere { \\\\ {0} \r\n",
txtPlanet.Text);

The first string.Format () line is okay but at runtime the last line
generates the following exception:

"Input string was not in a correct format."

It's essentially same as its predecessor except for the inclusion of
the left curly-brace which, apparently, isn't escapable nor does it
appear that it needs to be and yet.....

Any suggestions as to what's wrong with this ocde?

Thanks,
Lilith
Jul 2 '08 #1
6 2903
Lilith <li****@dcccd.eduwrote:
I'm trying to build a string for eventual output to a file. Input
comes from a number of input boxes but I've only test on one text box
content when I get an exception at runtime. The program segment is:

string work;

work = string.Format("//------- {0} ------\r\n", txtPlanet.Text);
work += string.Format("sphere { \\\\ {0} \r\n",
txtPlanet.Text);

The first string.Format () line is okay but at runtime the last line
generates the following exception:

"Input string was not in a correct format."

It's essentially same as its predecessor except for the inclusion of
the left curly-brace which, apparently, isn't escapable nor does it
appear that it needs to be and yet.....

Any suggestions as to what's wrong with this ocde?
The left brace *is* escapable - with another left brace. Try this:

work += string.Format("sphere {{ \\\\ {0} \r\n",
txtPlanet.Text);

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 2 '08 #2
On Wed, 2 Jul 2008 22:37:32 +0100, Jon Skeet [C# MVP]
<sk***@pobox.comwrote:
>Lilith <li****@dcccd.eduwrote:
>I'm trying to build a string for eventual output to a file. Input
comes from a number of input boxes but I've only test on one text box
content when I get an exception at runtime. The program segment is:

string work;

work = string.Format("//------- {0} ------\r\n", txtPlanet.Text);
work += string.Format("sphere { \\\\ {0} \r\n",
txtPlanet.Text);

The first string.Format () line is okay but at runtime the last line
generates the following exception:

"Input string was not in a correct format."

It's essentially same as its predecessor except for the inclusion of
the left curly-brace which, apparently, isn't escapable nor does it
appear that it needs to be and yet.....

Any suggestions as to what's wrong with this ocde?

The left brace *is* escapable - with another left brace. Try this:

work += string.Format("sphere {{ \\\\ {0} \r\n",
txtPlanet.Text);
Ach! I should have thought of that. I was trying the standard whack.
I did a work around with

work += string.Format("sphere {0} \\\\ {1} \r\n",
'{', txtPlanet.Text);

which worked but feels like a hack.

--
Thanks.
Lilith
Jul 2 '08 #3
The left brace *is* escapable - with another left brace. Try this:

Pardon my ignorance, but is there any particular reason why the "{"
can’t be escaped the "natural" way? Such as "\{"?
Jul 2 '08 #4
qg**********@mailinator.com wrote:
>The left brace *is* escapable - with another left brace. Try this:

Pardon my ignorance, but is there any particular reason why the "{"
can’t be escaped the "natural" way? Such as "\{"?
There is natural and there is natural.

I believe there are as many languages that uses doubling
of the character that needs to be escaped as there are
using \.

C# uses \, but this is a .NET issue not a C# issue. The VB.NET
guys will not consider \ natural (unless they also do C#).

Arne
Jul 2 '08 #5
<qg**********@mailinator.comwrote:
The left brace *is* escapable - with another left brace. Try this:

Pardon my ignorance, but is there any particular reason why the "{"
can=3Ft be escaped the "natural" way? Such as "\{"?
Yup - the backslash is the C# way of escaping a character within a
string. It affects the characters within a string. Here, the string
itself needs to contain both backslashes - because it's being
interpreted by the formatting engine.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 2 '08 #6
Jon Skeet [C# MVP] <sk***@pobox.comwrote:
<qg**********@mailinator.comwrote:
The left brace *is* escapable - with another left brace. Try this:
Pardon my ignorance, but is there any particular reason why the "{"
can=3Ft be escaped the "natural" way? Such as "\{"?

Yup - the backslash is the C# way of escaping a character within a
string. It affects the characters within a string. Here, the string
itself needs to contain both backslashes - because it's being
interpreted by the formatting engine.
Sorry - both braces, not backslashes. As Arne says, it's .NET
interpreting the string, not C# itself.

Having said that, using backslash would have been consistent with its
use in regular expressions...

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 2 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michelle Hillard | last post by:
Hi guys, would appreciate if you can shed some light on this. Sorry to be a pain, can you tell me what is wrong with the following: for /F %%i in ('dir /b /on c:\bcp\pc*.txt') do bcp...
5
by: m_t_hill | last post by:
Running MS Access 2000 MS Windows XP Pro This has caused me a lot of hair loss in the last few days so would appreciate any help. I am running code to append/update a local access database...
11
by: Mark Findlay | last post by:
Hello Experts! I am attempting to use the OleDbCommand.ExecuteScaler() function within my ASP.NET C# web page to perform a simple validation, but receive the following error: "Incorrect...
1
by: tshad | last post by:
I have a field that I read from my database that is a bit field field into a textbox. The Trace on the variable (MyInfoCreated) shows it as "False" - as you can see from the error. I tried it...
4
by: Tom | last post by:
Hello everyone, I found a frustrating behavior in the TreeView control in .NET 2.0. The TreeNode that is passed in the TreeNodeMouseClickEventArgs is incorrect when the TreeView automatically...
0
by: hudhuhandhu | last post by:
have got an error which says Input string was not in a correct format. as follows.. Description: An unhandled exception occurred during the execution of the current web request. Please review the...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
11
by: RipperT | last post by:
Don't know if this group covers web apps, but here goes. In VS 2005, I am trying to get variables to hold thier values during postback from the server. I convert a text box's user-keyed value to an...
1
by: ndawg123 | last post by:
Hey guys what im trying to do is write a yatzee game with C. And im stuck already and its the start?!?! I want the user to type there 5 numbers. i.e My program so far does this Please...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.