@inherits umbraco.MacroEngines.DynamicNodeContext
@using System.Collections;
@{ List<string> qa = new List<string>(); } //this is not defined in the recursive helper below
@helper traverseFirst(dynamic node){
var items = node.Children.Where("umbracoNaviHide != true");
foreach (var item in items) {
foreach(var subItem in item.Descendants()) {
if(subItem.Id == Model.Id)
{
qa.Add();
break;
}
}
@traverseFirst(item)
}
}
@traverseFirst(@Model.AncestorOrSelf("Book"))
The variable qa canot be accessed in the recursive helper. Is there a way around this?
Nothing physical happens. A typical implementation will allocate enough space in the program stack to store all variables at the deepest level of block nesting in the current function. This space is typically allocated in the stack in one shot at the function startup and released back at the function exit.
To declare a variable in the View using Razor syntax, we need to first create a code block by using @{ and } and then we can use the same syntax we use in the C#. In the above code, notice that we have created the Code block and then start writing C# syntax to declare and assign the variables.
In simple terms, scope of a variable is its lifetime in the program. This means that the scope of a variable is the block of code in the entire program where the variable is declared, used, and can be modified.
No, you cannot redefine a variable with different types within the same scope.
Define the variable in a @functions
section.
The normal @{
places your code in some method body. Use @functions
to define class members.
@functions{ List<string> qa = new List<string>(); }
More reading on this matter: SLaks Dissecting razor series.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With