Like a number or a string, a Boolean value can be stored in a variable. To declare such a variable, use the Boolean keyword. Here is an example: <%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim EmployeeIsMarried As Boolean %> </body> </html> To actually use a Boolean variable, you can assign a value to it. By default, if you declare a Boolean variable but do not initialized it, it receives a value of False: <%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim EmployeeIsMarried As Boolean Response.Write("Employee Is Married? " & EmployeeIsMarried) %> </body> </html> This would produce:
To initialize a Boolean variable, assign it a True or a False value. In the Visual Basic language, a Boolean variable can also deal with numeric values. The False value is equivalent to 0. For example, instead of False, you can initialize a Boolean variable with 0. Any other numeric value, whether positive or negative, corresponds to True: <%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim EmployeeIsMarried As Boolean EmployeeIsMarried = -792730 Response.Write("Employee Is Married? " & EmployeeIsMarried) %> </body> </html> The number can be decimal or hexadecimal: <%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim EmployeeIsMarried As Boolean EmployeeIsMarried = &HFA26B5 Response.Write("Employee Is Married? " & EmployeeIsMarried) %> </body> </html>
Boolean values can be involved with procedures. This means that a Boolean variable can be passed to a procedure and/or a function can be made to return a Boolean value.
To pass an argument as a Boolean value, in the parentheses of the procedure, type the name of the argument followed by the As Boolean expression. Here is an example: Private Sub CheckingEmployee(ByVal IsFullTime As Boolean) End Sub In the same way, you can pass as many Boolean arguments as you need, and you can combine Boolean and non-Boolean arguments as you judge necessary. Then, in the body of the procedure, use (or do not use) the Boolean argument as you wish.
You can create a function that returns a Boolean value. When declaring the function, specify its name and the As Boolean expression on the right side of the parentheses. Here is an example: Public Function IsDifferent() As Boolean End Function Of course, the function can take arguments of any kind you judge necessary: Public Function IsDifferent(ByVal Value1 As Integer, _ ByVal Value2 As Integer) As Boolean End Function Before exiting the function, you must return a value that evaluates to True or False. We will see an example below.
To convert a value to Boolean, you can use the CBool() function. Its syntax is: Function CBool(ByVal Expression As Object) As Boolean The CBool function takes as argument the expression to be evaluated. It should produce a valid Boolean value. If it does, the function returns True or False.
One of the most valuable operations you will perform on a value consists To find out whether a value is numeric or not, you can call the IsNumeric() function. Its syntax is: Public Function IsNumeric(ByVal Expression As Object) As Boolean This function takes as argument the value or expression to be evaluated. If the argument holds or can produce a valid integer or a decimal value, the function returns True. Here is an example: <%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim Value As Object Value = 258.08 * 9920.3479 Response.Write("Is Numeric? " & IsNumeric(Value)) %> </body> </html> This would produce: If the argument is holding any other value that cannot be identified as a number, the function produces False. Here is an example: <%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim Value As Object Value = #12/4/1770# Response.Write("Is Numeric? " & IsNumeric(Value)) %> </body> </html>
To find out whether an expression holds, or doesn't hold, a valid date, a valid, you can call the IsDate() function. Its syntax is: Public Function IsDate(ByVal Expression As Object) As Boolean This function takes an argument as the expression to be evaluated. If the argument holds a valid date and/or time, the function returns True. Here is an example: <%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim DateHired As Object DateHired = "9/16/2001" Response.Write("Is Date? " & IsDate(DateHired)) %> </body> </html> This would produce: If the value of the argument cannot be evaluated to a valid date or time, the function returns False. Here is an example: <%@ Page Language="VB" %> <html> <head> <title>Exercise</title> </head> <body> <% Dim DateHired As Object DateHired = "Who Knows?" Response.Write("Is Date? " & IsDate(DateHired)) %> </body> </html>
To find out whether a variable or an expression holds a Nothing value, you can call the IsNothing() function. Its syntax is: Public Function IsNothing(ByVal Expression As Object) As Boolean When calling this function, you can pass it a value or an expression. If the argument holds a valid value, this function returns False. If the argument does not hold a value, this function produces True. |
|
|||||||||||||||||||||||||||||||
|