I have defined a class that caches the value from the database and added implicit conversions so it works as a boolean variable. (This class is used to get/set user preferences in our projects)
public class BoolParameter
{
private static string _ParameterName;
private static bool? _CurrentVal;
public BoolParameter(string ParameterName)
{
_ParameterName = ParameterName;
}
public bool Val
{
get
{
return
(bool)
(_CurrentVal ??
(_CurrentVal =
DataBaseAccesor[_ParameterName] == "Y"));
}
set
{
_CurrentVal = value;
DataBaseAccesor[_ParameterName] = value ? "Y" : "N";
}
}
//setter doesnt work, how to define the implicit operator setter without passing the parametername?
/* static public implicit operator BoolParameter(bool value)
{
//being a new BoolParameter it would need parametername
return new BoolParameter(value);
}*/
//getter works fine
static public implicit operator bool(BoolParameter parameter)
{
return parameter.Val;
}
}
Usage:
BoolParameter parshowmessageafterupdate=new BoolParameter("UserParShowMessageAfterUpdate");
//later use
if(parshowmessageafterupdate)
{
//show message x
}
//or later in Options Screen set the preference
//this part is what doesnt work
//parshowinitialmessage=true;
//i would have to do this instead:
parshowinitialmessage.Val=true;
How to define the implicit operator setter without passing the parametername? Would this cache class be optimum vs accessing the database directly each time you read/get the parameter?
Why the nullable bool?
Couldn't you use: