A
Andrew
I am creating a number of calculation functions which will exist
within a class object. The properties used in the function may not
have been given a value so I need an efficient way to determine this
and acting accordingly. I can see about 3 options - all with
drawbacks - so am hoping someone could give me some guidance on which
is good practice and of course if there is a better alternative! I
wonder if vb.net offers any insights given that it doesn't support
variants? My options are:
1) Declare variables as variant and initialise to Null.
Now I can simply carry out the calculation and if any of the variables
is null the function will return Null. Very straight foward but I
would normally steer away from using variants if possible.
2) Declare variables as double and initialise to a value representing
no value (eg. -999)
Now I can test each of the variables before doing the calculation.
This itself seems to be somewhat of an overhead though and also means
that when I call other properties (which are themselves calculations)
I need to pass them first to a variable and test them rather than
using them directly in my function. If I could use zero as my "no
value" constant it would work in much the same way as Null for
variants but for division would give an error.
3) Create my own variable type as follows
Type Number
val as double
hasval as integer '0 if no, 1 if yes
end Type
Now I can multiply .hasval for each property and if any of them are 0
it will return 0.
Very interested to hear how others have handled this issue since it
must have come up before.
Thanks a lot,
Andrew
within a class object. The properties used in the function may not
have been given a value so I need an efficient way to determine this
and acting accordingly. I can see about 3 options - all with
drawbacks - so am hoping someone could give me some guidance on which
is good practice and of course if there is a better alternative! I
wonder if vb.net offers any insights given that it doesn't support
variants? My options are:
1) Declare variables as variant and initialise to Null.
Now I can simply carry out the calculation and if any of the variables
is null the function will return Null. Very straight foward but I
would normally steer away from using variants if possible.
2) Declare variables as double and initialise to a value representing
no value (eg. -999)
Now I can test each of the variables before doing the calculation.
This itself seems to be somewhat of an overhead though and also means
that when I call other properties (which are themselves calculations)
I need to pass them first to a variable and test them rather than
using them directly in my function. If I could use zero as my "no
value" constant it would work in much the same way as Null for
variants but for division would give an error.
3) Create my own variable type as follows
Type Number
val as double
hasval as integer '0 if no, 1 if yes
end Type
Now I can multiply .hasval for each property and if any of them are 0
it will return 0.
Very interested to hear how others have handled this issue since it
must have come up before.
Thanks a lot,
Andrew