Here is the code:
for (int i = 0; i < Model.Count; i++)
{
Writer.WriteBeginTag("li");
if (i % 2 == 1)
{
string myVar = "even ";
}
if ((i+1) % 3 == 0)
{
myVar += "third";
}
Writer.WriteAttribute("class", ""); // I want to use myVar here
Writer.Write(HtmlTextWriter.TagRightChar);
Writer.WriteEndTag("li");
}
I’m not all that familiar with .NET or C#. I get an error The name "myVar" does not exist in the current context on the second myVar. If I comment this line out, I then have a message The variable "myVar" is assigned but its value is never used. on the first myVar. I don’t really understand how or why this would be out of context/scope.
Any ideas?
myVaronly exists inside yourif.You need to move the declaration to the body of the loop.