Design question
-
Hi everybody! I'm currently working on a class that has some public properties of type
double
. For some of them i have to restrict the set accessor, so that only values greater 0 can be assigned. Currently I'm ignoring an assigned value that is smaller 0public double Alpha
{
set
{
if (value > 0)
this.alpha = value;
}
}but I'm not really happy with this solution, cause the user has no feedback if the value specified by him is really assigned. Is it good practice to add an else-block and throw an
ArgumentOutOfRangeException
there? If so, should i offer some method to check if a double value would be valid for alpha or some static min and max fields? Thanks in advance!
-
Hi everybody! I'm currently working on a class that has some public properties of type
double
. For some of them i have to restrict the set accessor, so that only values greater 0 can be assigned. Currently I'm ignoring an assigned value that is smaller 0public double Alpha
{
set
{
if (value > 0)
this.alpha = value;
}
}but I'm not really happy with this solution, cause the user has no feedback if the value specified by him is really assigned. Is it good practice to add an else-block and throw an
ArgumentOutOfRangeException
there? If so, should i offer some method to check if a double value would be valid for alpha or some static min and max fields? Thanks in advance!
You can add a bool variable which indicate if the value has been assigned. Like this:
public double Alpha { set { if (value > 0){ assigned = true; this.alpha = value; } } }
But im not sure i got your question! I hope it helps.. VentoEngine corp. Program your life ^^ -
Hi everybody! I'm currently working on a class that has some public properties of type
double
. For some of them i have to restrict the set accessor, so that only values greater 0 can be assigned. Currently I'm ignoring an assigned value that is smaller 0public double Alpha
{
set
{
if (value > 0)
this.alpha = value;
}
}but I'm not really happy with this solution, cause the user has no feedback if the value specified by him is really assigned. Is it good practice to add an else-block and throw an
ArgumentOutOfRangeException
there? If so, should i offer some method to check if a double value would be valid for alpha or some static min and max fields? Thanks in advance!
This is a design decision that depends on many factors. However, generally speaking, if it were me and this variable was exposed to the user via the UI, I would put my "feedback" mechanism directly into the UI rather than putting it into the class itself. That is, if the user defined this value in a textbox, for example, I would write the textbox in such a way that it simply would not allow the user to enter an incorrect value. I would then put something into the help file to explain the behavior. I think that is better than cluttering up your classes with a lot of exception crap and the UI with pop up warning boxes etc.
-
Hi everybody! I'm currently working on a class that has some public properties of type
double
. For some of them i have to restrict the set accessor, so that only values greater 0 can be assigned. Currently I'm ignoring an assigned value that is smaller 0public double Alpha
{
set
{
if (value > 0)
this.alpha = value;
}
}but I'm not really happy with this solution, cause the user has no feedback if the value specified by him is really assigned. Is it good practice to add an else-block and throw an
ArgumentOutOfRangeException
there? If so, should i offer some method to check if a double value would be valid for alpha or some static min and max fields? Thanks in advance!
One solution indeed could be to throw an exception when an invalid value is entered. But you should capture this error in the user interface before the data is entered into the model you are using. The exception is only for the other developers that use your model in their applications. WM.
What about weapons of mass-construction? -
This is a design decision that depends on many factors. However, generally speaking, if it were me and this variable was exposed to the user via the UI, I would put my "feedback" mechanism directly into the UI rather than putting it into the class itself. That is, if the user defined this value in a textbox, for example, I would write the textbox in such a way that it simply would not allow the user to enter an incorrect value. I would then put something into the help file to explain the behavior. I think that is better than cluttering up your classes with a lot of exception crap and the UI with pop up warning boxes etc.
At first thanks for the answer. The class I'm writing is part of a class library, so the variable may be exposed to the user via GUI but didn't necessarily have to. So I'm searching for a way, to let an user of the class check for validity of an assigned value no matter if it happens directly in code or via GUI. Currently I favor the following solution:
public double Alpha
{
set
{
if (value <= 0)
throw new ArgumentOutOfRangeException("Assigned value has to be greater 0");this.alpha = value;
}
}
public bool IsValidAlpha(double value)
{
return value > 0;
}
}//User of the class then can do
if (obj.IsValidAlpha(1.0))
obj.Alpha = 1.0;
else
...
www.troschuetz.de -- modified at 5:41 Thursday 1st December, 2005
-
One solution indeed could be to throw an exception when an invalid value is entered. But you should capture this error in the user interface before the data is entered into the model you are using. The exception is only for the other developers that use your model in their applications. WM.
What about weapons of mass-construction?At first thanks for the answer. The class I'm writing is part of a class library, so I don't write a GUI myself. I'm searching for a way, to let an user of the class check for validity of an assigned value no matter if it happens directly in code or via GUI. Currently I favor the following solution:
public double Alpha
{
set
{
if (value <= 0)
throw new ArgumentOutOfRangeException("Assigned value has to be greater 0");this.alpha = value;
}
}
public bool IsValidAlpha(double value)
{
return value > 0;
}
}//User of the class then can do
if (obj.IsValidAlpha(1.0))
obj.Alpha = 1.0;
else
...
-
At first thanks for the answer. The class I'm writing is part of a class library, so I don't write a GUI myself. I'm searching for a way, to let an user of the class check for validity of an assigned value no matter if it happens directly in code or via GUI. Currently I favor the following solution:
public double Alpha
{
set
{
if (value <= 0)
throw new ArgumentOutOfRangeException("Assigned value has to be greater 0");this.alpha = value;
}
}
public bool IsValidAlpha(double value)
{
return value > 0;
}
}//User of the class then can do
if (obj.IsValidAlpha(1.0))
obj.Alpha = 1.0;
else
...
-
You should be calling IsValidAlpha from within the setter instead of duplicating the code.