In the first chapter on the vb .net book I’m using, they have this code:
<%
Dim f As Double
For f = 50.0 To 99.0 Step 1.0
’ Sends formatted output to HTTP response
Response.output.Write("<tr><td>{0}</td><td>" & “{1:f}</td><tr>”, f,ToCelsius(f))
Next
%>
The code creates a two column table with one column temperateure in Fahrenheit and the other the temperature converted to Celsius. Conversion done via the ToCelsius() funtion.
I’m having trouble with the response.output.write syntax. The part I don’t get is how to use the format string. tr, td, ampersand, I get. Why the curly braces? Why the {0} then the {1:f}?
If I change the {0} to a y, then the first colum all turns into y’s. Why does the {0} return the f value?
I’m used to VBA and in there curly braces meant arrays. What do they mean in vb .Net?
these guys are used as series placeholders… so {0} means ‘use the first value in the additional parameters’, and {1:f} means ‘use the second value in the additional parameters, in format f’ (f is fixed-point format.)
So, it plugs in f as 0, and toCelsius(f) as 1, because those are the additional parameters supplied.
There’s no particular reason you need to use this syntax here if you don’t like it. You could have replaced it with:
Response.Write("<tr><td>" & f & "</td><td>")
Response.Write(formatNumber(toCelsius(f), 2) & "</td><tr>")
I’m just wondering why the ampersand is in the example. Doesn’t it just concatenate the two strings? If so, wouldn’t “<tr><td>{0}</td><td>{1:f}</td><tr>” work just as well? Does ASP .NET require placeholders to be in separate strings? Or is there some more mundane reason? Did they just want to demonstrate the ampersand in action? Or perhaps the statement is split into two lines in the book because its columns are too narrow?
That was what I was hinting at when I said it was written awkwardly. AFAIK it doen’t do anything here except join the two strings. I think it needlessly confuses the issue.