[quoted text, click to view] "Anonymous" <anonymous@discussions.microsoft.com> wrote in message
news:053f01c3d4b2$15a025b0$a301280a@phx.gbl...
> This is a pretty dumb question but I can't seem to find
> answer.
>
> How do you check to see if a datatype of a variable is
> correct? Eg in Visual Basic:
>
> Dim datevar
> datevar= "1a/2/2004"
>
> If IsDate(datevar) <> true then
> msgbox "The value in variable 'datevar' is not a date."
> End if
>
> Thanks.
First, declaring a variable as a specific type is a good idea. That helps
with the process of checking input. Here is a simple VB.NET routine to do
what you want:
(this is from a button click event handler once the user has entered a date
(hopefully) in TextBox1)
Dim dateVar As Date
Try
dateVar = TextBox1.Text
Catch ex As Exception
'We get here if the entry was not a date
TextBox1.Text = ""
TextBox1.Focus()
MessageBox.Show("please enter a date")
End Try
This is very basic, but it should get you started with try - catch blocks.
--
Peter [MVP Academic]