M
Mark Broadbent
C# 's type checking is great, but one thing which has reared it's ugly head
is the possibility of causing an unhandled Exception because you've passed
in an un-initialised or empty object. In some cases this might be a valid
operation in your class but many times it might not.
Say for instance I have a class Widget with only one constructor...
class Widget{
private string widgetName;
public Widget( string widgetname) {
widgetName = widgetname;
}
public string WidgetName{
get {return widgetName}
}
}
Now operationally an instance of this class will be ok if it is created like
so...
Widget myWidget = new Widget(null);
or
Widget myWidget = new Widget("");
But logically speaking it would be a nonsense. We might also expand on the
implementaion of the class so that the workings of it depend on the
constructor string being i) Initialised and ii) Having at least one
character otherwise an unhandled/ unexpected exception might be thrown. For
instance class members might (and indeed in most cases) assume that the
widgetName private variable is set to a value.
As far as I am aware, there is no current mechanism in place to prevent this
happening (or in otherwords to programatically constrain parameters so that
the C# compiler reports back any invalid inputs). The only way around this
is for each method that this might be an issue to check the parameters at
the mehtod begining and throw a custom exception to indicate this "breaking
of the rules" (but of course this issue then becomes a run-time thing).
.....So my questions are these
i.
Is there a better mechanism currently in place to already do this?
(e.g. maybe an attribute, for instance something like this
[parameter("widgetname", Constrained = true, Constraint.Length > 0)]
public Widget( string widgetname)
ii.
If this is not a feature of the current runtime, is something like it going
to be available in 2.0 or higher?
Thanks,
Br,
Mark.
is the possibility of causing an unhandled Exception because you've passed
in an un-initialised or empty object. In some cases this might be a valid
operation in your class but many times it might not.
Say for instance I have a class Widget with only one constructor...
class Widget{
private string widgetName;
public Widget( string widgetname) {
widgetName = widgetname;
}
public string WidgetName{
get {return widgetName}
}
}
Now operationally an instance of this class will be ok if it is created like
so...
Widget myWidget = new Widget(null);
or
Widget myWidget = new Widget("");
But logically speaking it would be a nonsense. We might also expand on the
implementaion of the class so that the workings of it depend on the
constructor string being i) Initialised and ii) Having at least one
character otherwise an unhandled/ unexpected exception might be thrown. For
instance class members might (and indeed in most cases) assume that the
widgetName private variable is set to a value.
As far as I am aware, there is no current mechanism in place to prevent this
happening (or in otherwords to programatically constrain parameters so that
the C# compiler reports back any invalid inputs). The only way around this
is for each method that this might be an issue to check the parameters at
the mehtod begining and throw a custom exception to indicate this "breaking
of the rules" (but of course this issue then becomes a run-time thing).
.....So my questions are these
i.
Is there a better mechanism currently in place to already do this?
(e.g. maybe an attribute, for instance something like this
[parameter("widgetname", Constrained = true, Constraint.Length > 0)]
public Widget( string widgetname)
ii.
If this is not a feature of the current runtime, is something like it going
to be available in 2.0 or higher?
Thanks,
Br,
Mark.