473,321 Members | 1,778 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,321 software developers and data experts.

Emulating String.Format compile-time functionality

With String.Format, if I have an incorrect number of args specified for a
format string, compile fails. How can I implement similar design-time
functionality for my own string functions?

Apr 10 '07 #1
7 3044
Rick wrote:
With String.Format, if I have an incorrect number of args specified for a
format string, compile fails.
No it doesn't. This compiles just fine for me, but fails at runtime:

---8<---
~$ cat test.cs
class App
{
static void Main()
{
string.Format("{0} {1}", "foo");
}
}

~$ csc test.cs
Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.

~$ ./test

Unhandled Exception: System.FormatException: Index (zero based) must be
greater than or equal to zero and less than the size of the argument
list.
at System.Text.StringBuilder.AppendFormat(IFormatProv ider provider,
String format, Object[] args)
at System.String.Format(IFormatProvider provider, String format,
Object[] args)
at App.Main()
--->8---

-- Barry

--
http://barrkel.blogspot.com/
Apr 10 '07 #2
"Barry Kelly" <ba***********@gmail.comwrote in message
news:ua********************************@4ax.com...
Rick wrote:
>With String.Format, if I have an incorrect number of args specified
for a
format string, compile fails.

No it doesn't. This compiles just fine for me, but fails at runtime:
<snip>

This is one of my little pet peeves.

This compiles just fine
Console.WriteLine("{0)", 3); // not the mismatched braces

but then you get the dreaded
System.FormatException: Input string was not in a correct format.

I realize that the method call is syntactically correct, but it will
obviously fail at runtime.
It would be nice if the compiler went that extra mile and validated the
format string at compile time.
Obviously, this couldn't be done every time.
For instance, if the format string was dynamic.
But if the format string is a string literal, there is nothing (that I
can see) that would prevent the compiler from "Looking" at the format
string and the argument list.

Maybe in 3.0

Bill
Apr 10 '07 #3
On Apr 11, 12:53 am, "Bill Butler" <qwe...@asdf.comwrote:

<snip>
This is one of my little pet peeves.

This compiles just fine
Console.WriteLine("{0)", 3); // not the mismatched braces

but then you get the dreaded
System.FormatException: Input string was not in a correct format.

I realize that the method call is syntactically correct, but it will
obviously fail at runtime.
It would be nice if the compiler went that extra mile and validated the
format string at compile time.
I can see the benefits, but I think a compiler should be sticking to
the language specification - and the language spec *certainly*
shouldn't be talking about format specifiers and the like, IMO. That's
the business of the library being used, not the language.
Obviously, this couldn't be done every time.
For instance, if the format string was dynamic.
But if the format string is a string literal, there is nothing (that I
can see) that would prevent the compiler from "Looking" at the format
string and the argument list.
There's nothing language specific about this - I think it would be
quite possible for a tool to run through a built assembly and check
for string literals which are used for format strings. As well as a
knowledge of the current methods taking format strings, you could also
add a decorator attribute to a parameter to specify that it should be
a format string, so that the tool could validate the use of your own
methods as well.

Jon

Apr 11 '07 #4
"Barry Kelly" <ba***********@gmail.comwrote in message
news:ua********************************@4ax.com...
Rick wrote:
>With String.Format, if I have an incorrect number of args specified for a
format string, compile fails.

No it doesn't. This compiles just fine for me, but fails at runtime:
Ooops! Then it's Resharper doing it for me.

Apr 11 '07 #5
Using Visual Studio without R# would be like going back to C++.

///ark
Apr 11 '07 #6
Bill Butler wrote:
This is one of my little pet peeves.

This compiles just fine
Console.WriteLine("{0)", 3); // not the mismatched braces

but then you get the dreaded
System.FormatException: Input string was not in a correct format.

I realize that the method call is syntactically correct, but it will
obviously fail at runtime.
It would be nice if the compiler went that extra mile and validated the
format string at compile time.
Probably the "best" way to do it would to enable compile-time code
execution of some kind. A little like D's system, perhaps.

I believe something like that is some way out though.

-- Barry

--
http://barrkel.blogspot.com/
Apr 11 '07 #7
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
On Apr 11, 12:53 am, "Bill Butler" <qwe...@asdf.comwrote:
>This is one of my little pet peeves.

This compiles just fine
Console.WriteLine("{0)", 3); // not the mismatched braces

but then you get the dreaded
System.FormatException: Input string was not in a correct format.

I realize that the method call is syntactically correct, but it will
obviously fail at runtime.
It would be nice if the compiler went that extra mile and validated
the
format string at compile time.

I can see the benefits, but I think a compiler should be sticking to
the language specification - and the language spec *certainly*
shouldn't be talking about format specifiers and the like, IMO. That's
the business of the library being used, not the language.
I know. I know. But we are talking about THE library.
The call IS syntactically correct, and therefore, beyond the job
description of a compiler, but since Console.WriteLine and all of it's
similar cousins are part of the CLR (and commonly used), it would be
helpful to have some means of validating all recognizable format strings
at compile time (or at least before the program ships).

A compiler switch for csc.exe would be nice, but I would be fine with a
separate command line tool.

>Obviously, this couldn't be done every time.
For instance, if the format string was dynamic.
But if the format string is a string literal, there is nothing (that
I
can see) that would prevent the compiler from "Looking" at the format
string and the argument list.

There's nothing language specific about this - I think it would be
quite possible for a tool to run through a built assembly and check
for string literals which are used for format strings. As well as a
knowledge of the current methods taking format strings, you could also
add a decorator attribute to a parameter to specify that it should be
a format string, so that the tool could validate the use of your own
methods as well.
You have a point, I could probably sit down and write something that
does exactly what I was asking Microsoft to give me for free :^)

Of course, if I wrote it, far fewer people would have access to it (and
it would have a greater chance of being half-assed).

Oh well

Thanks for the input
Bill


Apr 12 '07 #8

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

Similar topics

2
by: san | last post by:
Hello, all! I have question about String.Format method. There are two variants: public static string Format(string, params object); and public static string Format(IFormatProvider, string, params...
5
by: raffelm | last post by:
I'm struggling to find a way to include long path names in a command line argument string that I have to build at runtime. I need to create a string like -o:"c:\my documents\my file.txt". ...
5
by: sklett | last post by:
I have a simple debug and logging class and I would like to be able to send a non-formatted string to my various printing methods, much like string.format(). I have tried to implement this like...
2
by: Bob | last post by:
I'm having trouble the string.Format() throwing exceptions and I can't figure out what I am doing wrong. Given the following setup code: string str = { "one", "two", "three", "four" }; double...
7
by: Alpha | last post by:
Hi, I'm maintaining C# code and am fairly new with C# programming. I'm looking for codes that's droping the 2nd digit of a nuber printed out and I suspect it's the code below. Can someone tell me...
2
by: Dilbert | last post by:
This is the code snippet: .... int nTest = 10; String * pMsg = String::Format( "Display an integer here: {0}", nTest); .... I got the following error message when tried to compile the program:...
17
by: ramadu | last post by:
I know its a sin to use strings, lets skip that part... Which of these is faster and uses less memory? String.Format("SomeValue='{0}'", m_Value); or String.Concat("SomeValue='", m_Value,...
6
by: Scewbedew | last post by:
Suppose I have the following code: string myFormat = "Line1/nLine 2"; string formattedString = string.Format(myFormat); ....that would produce a 2-line output as expected. But if I load...
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
8
by: Armando Rocha | last post by:
Hi, Hi have a string with 16 chars "25DD68EDEB8D5E11" and i want show it in form like this "25DD-68ED-EB8D-5E11", i try String.Format("{0:####-####-####-####}", mystr), but not work, i think...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.