Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • James Feeney 26 posts 166 karma points
    Oct 10, 2013 @ 11:20
    James Feeney
    0

    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.

    @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 } } }
  • Dallas 132 posts 404 karma points
    Oct 10, 2013 @ 13:50
    Dallas
    101

    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
                }
            }
        }
    }
    
  • James Feeney 26 posts 166 karma points
    Oct 10, 2013 @ 17:01
    James Feeney
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft