Reputation: 33
I have the following snippet of code:
public static List<string> sqlData = new List<string>();
//
// lots of code here
//
if (/* check here to see if the sqlData[whatever] index exists */)
{
sqlData.Insert(count2, sqlformatted);
}
else
{
sqlData.Insert(count2, sqlformatted + sqlData[count2]);
}
What I want to know is how to check the index on sqlData to see if it exists before trying to insert something that contains itself.
Upvotes: 3
Views: 15262
Reputation: 7351
if(sqlData.Count > whatever )
{
//index "whatever" exists
string str = sqlData[whatever];
}
Upvotes: 1
Reputation: 838216
If whatever is always positive then you can use this:
if (whatever < sqlData.Count) { ... }
Or if whatever could also be negative then you need to add a test for that too:
if (whatever >= 0 && whatever < sqlData.Count) { ... }
Upvotes: 6