Infinity
Infinity

Reputation: 1325

why am I getting format exception in this code? It has one argument only

public override string ToString()
{
    string val;
    if (blower)
        val = "Yes";
    else
        val = "No";
    return string.Format(
                   "With Blower \t:\t {0} \n" +
                   val);
}

I am getting an exception in these lines:-

 return string.Format(
                "With Blower \t:\t {0} \n" +
                val);

The exception is:

Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

What am I doing wrong?

Upvotes: 0

Views: 197

Answers (7)

tbridge
tbridge

Reputation: 1804

You could simplify this entire method:

public override string ToString()
{
    return string.Format("With Blower \t:\t {0} \n", blower ? "Yes" : "No");
}

Upvotes: 3

ABH
ABH

Reputation: 3439

Use comma instead of concatenation

return string.Format("With Blower \t:\t {0} \n",  val);

Upvotes: 0

amdmax
amdmax

Reputation: 761

Use it this way:

string.Format("With Blower \t:\t {0} \n", val);

Upvotes: 17

J_D
J_D

Reputation: 3586

Try separating by a comma:

return string.Format( "With Blower \t:\t {0} \n", val);

Upvotes: 1

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25445

I think you meant

return string.Format("With Blower \t:\t {0} \n", val);
                                               ^

Upvotes: 10

Michael Schmeißer
Michael Schmeißer

Reputation: 3417

I think you need to replace the + with a comma maybe:

 return string.Format(
                "With Blower \t:\t {0} \n",
                val);

Upvotes: 1

Dave
Dave

Reputation: 1216

Do you mean to use a comma instead of a concatenation?

Upvotes: 3

Related Questions