I have this code:
Current.Resources["CardSetIconWidth"] = (width-30)/260 * 160;
I would like to ensure that Current.Resources["CardSetIconWidth"]
is always at least a minimum of 160.
Is there an easy way that I can do this?
Try this:
Current.Resources["CardSetIconWidth"] = Math.Max((width - 30) / 260 * 160, 160);
After the assignment, you could do
if (Current.Resources["CardSetIconWidth"]<160) Current.Resources["CardSetIconWidth"]=160;
Best regards
Another option would be to use @rfmodulator answer into the indexer of a class:
public class SafeValues
{
private readonly IDictionary<string, int> _dict = new Dictionary<string, int>();
public int this[string key]
{
get { return _dict[key]; } // you can also choose to use Math.Min
// in the getter to keep the original values in the underlying dictionary
// for debug purposes
set { _dict[key] = Math.Max(value, 160); }
}
}
So you can use it like
var Resources = new SafeValues(); // you can easily set a parameter for the min value in the constructor if you want
Resources["CardSetIconWidth"] = (width-30)/260 * 160;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.