More ASP .NET Newbie Help

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?

Thanks

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 think that would do pretty much the same thing.

I believe it to be awkwardly written.

The write statement takes this form:

Write(FormatString, argument1, argument2, … N)

The FormatString must have a curly brace pair for each arguement 1-N. It starts with {0}

So a format string might say “This is argument1{0}, this is argument1{1}”

Whatever you pass as argument1 replaces {0}

In your example, the ampersand joins two strings to create one format string. then it uses f as the first argument and ToCelsius as the second.

The {1:f} tell the format string to display the argument as a float. Look up the string class and its format function. It should explain all of this.

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.

Thank you all for clearing that up. I did not know where to find the syntax help. I will look in the string class help section tonight.