By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,244 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,244 IT Pros & Developers. It's quick & easy.

Emulating String.Format compile-time functionality

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
Using Visual Studio without R# would be like going back to C++.

///ark
Apr 11 '07 #6

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.