My only comment on this method of doing this is that, as I've said before, exceptions are expensive and should really not be used for the purpose of validating stuff. I suppose that if, for the most part, you'll only be getting valid data, then the exception will rarely be caught and you'll probably not get that hit all that much.
You decide. <g>
~~Bonnie
>I am given a byte value for month and a byte value for day. I want to verify that this is a correct date and instead of writing a lot of code to do this I figured I would just pass it to the DateTime constructor with the year equal to 2004 (because it was a leap year). So I do something like this:
>
>byte month = 0x01;
>byte day = 0x01;
>try
>{
> new System.DateTime(2004, month, day);
>}
>catch (Exception ex)
>{
> MessageBox.Show(ex.ToString(), "Month and/or day not valid");
>}
>
>
>I do not need the DateTime object so I do not assign a variable. Is this bad?
>
>Should I instead use the following code?
>
>byte month = 0x01;
>byte day = 0x01;
>try
>{
> System.DateTime dt = new System.DateTime(2004, month, day);
>}
>catch (Exception ex)
>{
> MessageBox.Show(ex.ToString(), "Month and/or day not valid");
>}
>
>
>Or doesn't it matter (estetics aside<s>)?
>
>If someone answers I will explain why I am wondering about such a trivial thing :)
>
>Einar