Another set of Visual Basic functions returns variables’ data types, but not the exact type. They return a True/False value indicating whether a variable holds a numeric value, a date or an array. The following functions are used to validate user input, as well as data stored in files, before you process them.
Returns True if its argument is a number (Short, Integer, Long, Single, Double, Decimal). Use this function to determine whether a variable holds a numeric value before passing it to a procedure that expects a numeric value or before processing it as a number. The following statements keep prompting the user with an InputBox for a numeric value. The user must enter a numeric value or click the Cancel button to exit. As long as the user enters non-numeric values, the Input box keeps popping up and prompting for a numeric value:
Dim strAge as String = ""
Dim Age As Integer
While Not IsNumeric(strAge)
strAge = InputBox("Please enter your age")
Age = Convert.ToInt16(strAge)
The variable strAge is initialized to a non-numeric value so that the While. . .End While loop will be executed at least once.
Returns True if its argument is a valid date (or time). The following expressions return True because they all represent valid dates:
IsDate("October 12, 2010")
If the date expression includes the day name, as in the following expression, the IsDate() function will return False:
IsDate("Sat. October 12, 2010") ' FALSE
Returns True if its argument is an array.
Why do we declare variables?
Visual Basic never enforced variable declaration (and it still doesn’t), which was a good thing for the beginner programmer. When you want to slap together a “quick-and-dirty” program, the last thing you need is someone telling you to decide which variables you’re going to use and to declare them before using them. This convenience, however, is a blessing in disguise because most programmers accustomed to the free format of Visual Basic also carry their habits of quick-and-dirty coding to large projects. When writing large applications, you will sooner or later discover that variable declaration is a necessity. It will help you write clean, strongly typed code and simplify debugging. Variable declaration eliminates the source of the most common and totally unnecessary bugs.
Let’s examine the side effects of using undeclared variables in your application. To be able to get by without declaring your variables, you must set the Explicit option to Off. Let’s assume that you’re using the following statements to convert Euros to U.S. dollars:
Euro2USD = 1.462
USDollars = amount * Euro2USD
The first time your code refers to the Euro2USD variable name, Visual Basic creates a new variable and then uses it as if it were declared.
Suppose that the variable Euro2USD appears in many places in your application. If in one of these places you type Euro2UDS, and the program doesn’t enforce variable declaration, the compiler will create a new variable, assign it the value zero, and then use it. Any amount converted with the Euro2UDS variable will be zero! If the application enforces variable declaration, the compiler will complain (the Euro2UDS variable hasn’t been declared), and you will catch the error right in the editor, as you type.