if/then/else variable frustration
-
Hello- I'm fairly new to C# (classic ASP guy), and I'm having a heckuva time with this. I am hoping someone can point out my flawed logic. I first need to ascertain if the current article group is one of several that need to be treated differently. If it is, it defines the var "strbody" using a complete value as retrieved from the database. If it is not one of those special article groups, then I have to do some string manipulation to format the retrieved value before defining and displaying it. My string manipulation code is flawless, but neither of the strbody vars I define in my if/then/else block is recognized when I call it below the code block???
@{
string group = Model.ArticleGroupName;if (group.Contains("Spacial Orientation")||group.Contains("Topography")||group.Contains("Osteology")||group.Contains("Angiology")||group.Contains("Neurology")||group.Contains("Myology")||group.Contains("Radiology")||group.Contains("Misc. Drawings")||group.Contains("Clinical Testing"))
{
var strbody = item.ShortBody;
}
else
{string s = item.ShortBody; string sLess = s.Remove(0, 12); int index = sLess.IndexOf("Summary"); var strbody = (sLess.Substring(index + 8)); }
}
@strbodyThat code results in the following error: \Plugins\FoxNetSoft.Articles\Views\ArticleRead\List.cshtml(76): error CS0103: The name 'strbody' does not exist in the current context I am new to this, so please don't hesitate to chastise me for doing dumb stuff...I need to learn! Thanks, Steve
-
Hello- I'm fairly new to C# (classic ASP guy), and I'm having a heckuva time with this. I am hoping someone can point out my flawed logic. I first need to ascertain if the current article group is one of several that need to be treated differently. If it is, it defines the var "strbody" using a complete value as retrieved from the database. If it is not one of those special article groups, then I have to do some string manipulation to format the retrieved value before defining and displaying it. My string manipulation code is flawless, but neither of the strbody vars I define in my if/then/else block is recognized when I call it below the code block???
@{
string group = Model.ArticleGroupName;if (group.Contains("Spacial Orientation")||group.Contains("Topography")||group.Contains("Osteology")||group.Contains("Angiology")||group.Contains("Neurology")||group.Contains("Myology")||group.Contains("Radiology")||group.Contains("Misc. Drawings")||group.Contains("Clinical Testing"))
{
var strbody = item.ShortBody;
}
else
{string s = item.ShortBody; string sLess = s.Remove(0, 12); int index = sLess.IndexOf("Summary"); var strbody = (sLess.Substring(index + 8)); }
}
@strbodyThat code results in the following error: \Plugins\FoxNetSoft.Articles\Views\ArticleRead\List.cshtml(76): error CS0103: The name 'strbody' does not exist in the current context I am new to this, so please don't hesitate to chastise me for doing dumb stuff...I need to learn! Thanks, Steve
"strbody" needs to be declared outside of the if statement, so that you can access it outside the scope of the if statement.
@{
string group = Model.ArticleGroupName;
string strbody = string.Empty;if (group.Contains("Spacial Orientation") || group.Contains("Topography") || group.Contains("Osteology") || group.Contains("Angiology") || group.Contains("Neurology") || group.Contains("Myology") || group.Contains("Radiology") || group.Contains("Misc. Drawings") || group.Contains("Clinical Testing"))
{
strbody = item.ShortBody;
}
else
{string s = item.ShortBody; string sLess = s.Remove(0, 12); int index = sLess.IndexOf("Summary"); strbody = (sLess.Substring(index + 8)); }
}
@strbody -
"strbody" needs to be declared outside of the if statement, so that you can access it outside the scope of the if statement.
@{
string group = Model.ArticleGroupName;
string strbody = string.Empty;if (group.Contains("Spacial Orientation") || group.Contains("Topography") || group.Contains("Osteology") || group.Contains("Angiology") || group.Contains("Neurology") || group.Contains("Myology") || group.Contains("Radiology") || group.Contains("Misc. Drawings") || group.Contains("Clinical Testing"))
{
strbody = item.ShortBody;
}
else
{string s = item.ShortBody; string sLess = s.Remove(0, 12); int index = sLess.IndexOf("Summary"); strbody = (sLess.Substring(index + 8)); }
}
@strbodyThanks for the response! I tried declaring the strbody as you suggested previously, but when I did, I got this error: A local variable named 'strbody' cannot be declared in this scope because it would give a different meaning to 'strbody', which is already used in a 'parent or current' scope to denote something else Learning C# is really frustrating!:confused: I could have done this exact thing with VBScript in about 2 minutes..and 8 less lines of code...
-
Thanks for the response! I tried declaring the strbody as you suggested previously, but when I did, I got this error: A local variable named 'strbody' cannot be declared in this scope because it would give a different meaning to 'strbody', which is already used in a 'parent or current' scope to denote something else Learning C# is really frustrating!:confused: I could have done this exact thing with VBScript in about 2 minutes..and 8 less lines of code...
Inside your if statement where it says "var strbody = ..." should now only be "strbody = ..." since you already declared the variable outside of the if statement. If you look at the modified code I provided in my previous post you will see that I am just assigning a value to the strbody variable inside of the if/else statement.
-
Inside your if statement where it says "var strbody = ..." should now only be "strbody = ..." since you already declared the variable outside of the if statement. If you look at the modified code I provided in my previous post you will see that I am just assigning a value to the strbody variable inside of the if/else statement.
I spotted that moments after submitting my reply... It's working perfectly now...thanks man!