Access different variable names on the fly
-
Hi all, Does someone know if it is possible to access a variable and change the name of the variable based on a condition. (This does not make much sense but let me explain it using the example below. I have two foreach loops, both doing the exact same thing. But it is accessing different variables. I would prefer it if i were to have one foreach loop and access the different variable based on the string that was passed to the function. Thus having something like (string.Concat(context,"ReadOnlyProperties"))
private void CustomizeEditor(string context) { DetailView detailViewCore = (DetailView)View; Object obj = detailViewCore.CurrentObject; if (context == "live") { foreach (string liveTargetPropertyName in liveReadOnlyProperties.Keys) { CriteriaOperator criteria = CriteriaOperator.Parse(liveReadOnlyProperties[liveTargetPropertyName]); UpdateProperty(detailViewCore, obj, liveTargetPropertyName, criteria); } } else { foreach (string saveTargetPropertyName in saveReadOnlyProperties.Keys) { CriteriaOperator criteria = CriteriaOperator.Parse(saveReadOnlyProperties[saveTargetPropertyName]); UpdateProperty(detailViewCore, obj, saveTargetPropertyName, criteria); } } }
Thanks
-
Hi all, Does someone know if it is possible to access a variable and change the name of the variable based on a condition. (This does not make much sense but let me explain it using the example below. I have two foreach loops, both doing the exact same thing. But it is accessing different variables. I would prefer it if i were to have one foreach loop and access the different variable based on the string that was passed to the function. Thus having something like (string.Concat(context,"ReadOnlyProperties"))
private void CustomizeEditor(string context) { DetailView detailViewCore = (DetailView)View; Object obj = detailViewCore.CurrentObject; if (context == "live") { foreach (string liveTargetPropertyName in liveReadOnlyProperties.Keys) { CriteriaOperator criteria = CriteriaOperator.Parse(liveReadOnlyProperties[liveTargetPropertyName]); UpdateProperty(detailViewCore, obj, liveTargetPropertyName, criteria); } } else { foreach (string saveTargetPropertyName in saveReadOnlyProperties.Keys) { CriteriaOperator criteria = CriteriaOperator.Parse(saveReadOnlyProperties[saveTargetPropertyName]); UpdateProperty(detailViewCore, obj, saveTargetPropertyName, criteria); } } }
Thanks
You can make a method which contains the loop, and call the method twice passing the variable to loop through..
.: I love it when a plan comes together :. http://www.zonderpunt.nl
-
Hi all, Does someone know if it is possible to access a variable and change the name of the variable based on a condition. (This does not make much sense but let me explain it using the example below. I have two foreach loops, both doing the exact same thing. But it is accessing different variables. I would prefer it if i were to have one foreach loop and access the different variable based on the string that was passed to the function. Thus having something like (string.Concat(context,"ReadOnlyProperties"))
private void CustomizeEditor(string context) { DetailView detailViewCore = (DetailView)View; Object obj = detailViewCore.CurrentObject; if (context == "live") { foreach (string liveTargetPropertyName in liveReadOnlyProperties.Keys) { CriteriaOperator criteria = CriteriaOperator.Parse(liveReadOnlyProperties[liveTargetPropertyName]); UpdateProperty(detailViewCore, obj, liveTargetPropertyName, criteria); } } else { foreach (string saveTargetPropertyName in saveReadOnlyProperties.Keys) { CriteriaOperator criteria = CriteriaOperator.Parse(saveReadOnlyProperties[saveTargetPropertyName]); UpdateProperty(detailViewCore, obj, saveTargetPropertyName, criteria); } } }
Thanks
private void CustomizeEditor(string context)
{
DetailView detailViewCore = (DetailView)View;
Object obj = detailViewCore.CurrentObject;
string parse, propertyName; // I am just assuming this is string. Change to your datatype
if (context == "live"){
parse = liveReadOnlyProperties[liveTargetPropertyName];
propertyName = liveTargetPropertyName;
keys = liveReadOnlyProperties.Keys;
}
else{
parse = saveReadOnlyProperties[saveTargetPropertyName];
propertyName = saveTargetPropertyName;
keys = saveReadOnlyProperties.Keys;
}foreach (string propertyName in keys)
{
CriteriaOperator criteria = CriteriaOperator.Parse(parse);
UpdateProperty(detailViewCore, obj, propertyName , criteria);
}
}Navaneeth How to use google | Ask smart questions
-
Hi all, Does someone know if it is possible to access a variable and change the name of the variable based on a condition. (This does not make much sense but let me explain it using the example below. I have two foreach loops, both doing the exact same thing. But it is accessing different variables. I would prefer it if i were to have one foreach loop and access the different variable based on the string that was passed to the function. Thus having something like (string.Concat(context,"ReadOnlyProperties"))
private void CustomizeEditor(string context) { DetailView detailViewCore = (DetailView)View; Object obj = detailViewCore.CurrentObject; if (context == "live") { foreach (string liveTargetPropertyName in liveReadOnlyProperties.Keys) { CriteriaOperator criteria = CriteriaOperator.Parse(liveReadOnlyProperties[liveTargetPropertyName]); UpdateProperty(detailViewCore, obj, liveTargetPropertyName, criteria); } } else { foreach (string saveTargetPropertyName in saveReadOnlyProperties.Keys) { CriteriaOperator criteria = CriteriaOperator.Parse(saveReadOnlyProperties[saveTargetPropertyName]); UpdateProperty(detailViewCore, obj, saveTargetPropertyName, criteria); } } }
Thanks
Why not simply pass in the appropriate collection?