Groups | Blog | Home
all groups > dotnet academic > january 2004 >

dotnet academic : Checking Datatype


Anonymous
1/6/2004 4:06:22 PM
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

Peter van der Goes
1/7/2004 7:18:00 AM

[quoted text, click to view]

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]

mohamoss
1/9/2004 8:35:15 AM
there are ways to chech that a variable is of a certain
type
first there is the IS operator that works with refernce
types
boolean ::= testobject Is some_reference_type
so this operator will return true if the object is of the
type and false otherwise



also theres is the AS operator that works also with
casting in c# {" it the casting is possible ")

[quoted text, click to view]
AddThis Social Bookmark Button