I have an if that checks for a querystring, If this equals "All" then collect everything into rep collection, If this equals (A certain item collection name) then collect only those that match into rep collection, Then do a for loop for the amount of items in the collection, Then do foreach for each item in collection.
Because I'm declaring the var rep inside an if statement, it's saying rep does not exist when I get to the for loop. I have tried to declare it outside (and from what I've read, this is considered anti-C#), but now I'm stuck.
Any help, greatly appreciated.
@using umbraco.MacroEngines
@inherits umbraco.MacroEngines.DynamicNodeContext
string collect = String.Format("{0}", Request.QueryString["QueryName"]); if (!string.IsNullOrEmpty(Request.QueryString["QueryName"])) { if (collect == "All") { var rep = CurrentModel.DescendantsOrSelf().Items.SingleOrDefault(); } else { var rep = CurrentModel.DescendantsOrSelf().Items.SingleOrDefault(x => x.Name == collect); } int i; for (i = 0; i < rep.Children.Count() / numInRow; i++) { // for loop required for positioning purposes foreach (var node in rep.Children) {
//do stuff with each node
}
}
}
As you experienced the rep variable does not exist when you get to the for loop. The variable is out of scope. rep needs to be declared outside of the if statement. It is possible for rep to be null in the code below if there are no descendants or none of the items match the collect value.
@using umbraco.MacroEngines
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
if (!string.IsNullOrEmpty(Request.QueryString["QueryName"]))
{
string collect = String.Format("{0}", Request.QueryString["QueryName"]);
DynamicNode rep = null;
if (collect == "All")
{
rep = CurrentModel.DescendantsOrSelf().Items.SingleOrDefault();
}
else
{
rep = CurrentModel.DescendantsOrSelf().Items.SingleOrDefault(x => x.Name == collect);
}
int i;
for (i = 0; i < rep.Children.Count()/numInRow; i++)
{
// for loop required for positioning purposes
foreach (var node in rep.Children)
{
//do stuff with each node
}
}
}
}
Cannot access variable inside If statement
I have an if that checks for a querystring,
If this equals "All" then collect everything into rep collection,
If this equals (A certain item collection name) then collect only those that match into rep collection,
Then do a for loop for the amount of items in the collection,
Then do foreach for each item in collection.
Because I'm declaring the var rep inside an if statement, it's saying rep does not exist when I get to the for loop. I have tried to declare it outside (and from what I've read, this is considered anti-C#), but now I'm stuck.
Any help, greatly appreciated.
As you experienced the rep variable does not exist when you get to the for loop. The variable is out of scope. rep needs to be declared outside of the if statement. It is possible for rep to be null in the code below if there are no descendants or none of the items match the collect value.
That worked a charm, thanks. Now I need to to figure this out! > http://our.umbraco.org/forum/developers/razor/45366-Collecting-multi-descendant-children
is working on a reply...