views:

83

answers:

5

I have this piece of code in c#:

private static void _constructRow(SqlDataReader reader, system.IO.StreamWriter stwr, bool getColumnName)
{
  for (int i = 0; i < reader.FieldCount; i++)
   stwr.Writeline(String.Format("<td>{0}</td"), getColumnName ? reader.GetName(i) : reader.GetValue(i).ToString()));
}

I'm trying to understand what the part that start with "getColumnName ?" and ends with ".ToString()" does. I understood that it is a system.object type, but I have no idea what it specifically does or how it works. I want that because of this: "reader" had multiple rows in it, and I want to writeline only specific rows.

If anyone can help me on either of those, I'd be grateful.

+3  A: 

This is a conditional operator. It says if getColumnName is true, then use reader.GetName(i) otherwise use reader.GetValue(i).ToString()

The format is this:

ThingToCheck ? UseIfCheckIsTrue : UseIfCheckIsFalse

In the code, it looks like getColumnName is true for the header row, so it's outputting the column name and called again for all other rows using false, to output the values.

Nick Craver
+1  A: 

The function iterates over all columns in the data reader, then for each one:

If getColumnName returns true, it outputs the name of the column between the <td> tags, otherwise the value of the data.

To de-construct further:

reader.GetName(i) - this returns the name of the column

reader.GetValue(i).ToString() - this returns the value of the column as a string

getColumnName - a function the will return true if a column name can be gotten

?: - the conditional operator. If the expression before the ? is true, the expression to the left of the : is used, otherwise the one on the right

String.Format("<td>{0}</td", arg) - this will output "<td>arg</td>" (btw - your code is wrong, the ) should not be just after the first string)
Oded
the most detailed answer, and I saw the ) bug as well thanks to this. Oded, Cheers and Toda!
jbkkd
A: 

That is called a conditional operator.

The argument getColumnName is evaluated and if true, the first argument after the ? is returned, if false, the second.

So, if getColumnName==true, you are going to see <td>NAME</td> else <td>Value</td>

Make sense?

Sky Sanders
A: 

It is like following

if (getColumnName == true)
{
    reader.GetName(i); // GetName is string so no need to convert this in string I.E ToString()
}
else
{
    reader.GetValue(i).ToString(); // GetValue returns object so this needs to convert in string using .ToString()
}

Because getColumnName is of bool type so no need to test it like

If (getColumnName == true)

You can write this as

If (getColumnName)

String.Format(string, method)

And String.Format method replaces items in specified string with given object, this method has two arguments first one is string and second is object. for example

string.Format("Question number {0} is answered by {1} {2}", 11, "Adam", "Yesterday");

The out put will be

Question number 11 is answered by Adam Yesterday

As you can see {0} is replaced with 11 and {1} is replaced with Adam and {2} is replaced with Yesterday.

you can read more about this here

Muhammad Kashif Nadeem
A: 

this is ternary operator, used for adhoc constitution of if else block.

vittore