How It Works

Crypto Ultimatum

How to Make Money with Bitcoin

Get Instant Access

Constants first are declared for the cost per unit for residents and nonresidents respectively. The actual values (11 and 50) could have been used in the code instead. However, using constants makes the code easier to change if the cost per unit ever changes, since only one change would need to be made (the value of the constant) rather than changing the value everywhere it is used in the code.

The remaining code branches in two directions, depending on whether the input of the number of units is valid (a positive number) or not (nonnumeric or a negative value). Since the input is either valid or not, an If...Then...Else statement works well. The If clause handles when the input is not valid, and the Else clause handles the circumstances in which the input is valid.

The If condition combines the Not operator with the OrElse operator: If Not IsNumeric(txtUnits.Text) _ OrElse CInt(txtUnits.Text) < 0 Then

The first condition is whether the input is a string representation of a number. The IsNumeric function is built into Visual Basic .NET. It returns True if the string that is its parameter (here the text entered in the text box) is a string representation of a number, and False if it is not. By using the Not operator, the first condition evaluates as True if the input is not numeric.

The second condition is whether the input is a negative number, and therefore invalid input. The type conversion keyword CInt, discussed in Module 5, is used before the comparison to convert the value of the text box control to an Integer.

The two conditions are in the disjunctive (or) rather than the conjunctive (and) because if the input is invalid if it either is not a number or is negative. The OrElse rather than Or operator is used because the CInt type conversion keyword in the second condition would cause an error if the first condition is True (the input is not a string representation of a number). For example, you could not use CInt to convert "Jeff" into an integer. With the OrElse operator, the second condition will not be evaluated if the first condition is True, avoiding this problem, which could cause an error if the Or operator were used instead.

If the If clause evaluates to True, it could be because the input either is not a number or is n egative. Therefore, another If.Else statement is nested within the If Clause to handle both circumstances and output the appropriate message to the StatusBar control:

If Not IsNumeric(txtUnits.Text) Then

StatusBar1.Text = "Number of units must be numeric"

Else

StatusBar1.Text = "Number of units must be positive" End If

The outer Else clause will only be reached if the input is valid. It covers two possibilities: the student either is a resident or is not. Therefore, an If.Else statement is nested within the Else clause:

Else

If chkResident.Checked Then lblTuition.Text = Format(CInt(txtUnits.Text) * RESIDENT, "c") Else lblTuition.Text = Format(CInt(txtUnits.Text) * NONRESIDENT, "c") End If

StatusBar1.Text = "Calculation successful" End If

Before multiplying the number of units by the cost per units, the number of units, obtained from the Text property of a TextBox control, must be converted from a string to a numeric data type. This is a narrowing conversion, and therefore (if Option Strict is on) requires a type conversion to a numeric data type. This type conversion is accomplished by the CInt type conversion keyword.

The Format function returns a string, formatted as specified in the parameters in the function. Here, the formatting is as currency, but there are other predefined formats such as date and time. Additionally, you can create user-defined formats, such as changing a string of nine digits ("123456789") into a Social Security number (" 123-45-6789").

The syntax of the Format function is:

Format([Expression to be formatted, [symbol specifying type of formatting]]

The Format function often is on the right side of an assignment statement, since it returns a formatted string, which then is assigned to a string variable on the left side of the assignment statement.

Note The Format function also has two additional optional parameters that are used only when the formatting is to specify a date and time.

The first parameter of the Format function is the result of the multiplication of the number of units by the cost per units. The second parameter specifies the type of formatting (currency in this case). Table 7-1 lists the predefined numeric formats that can be specified in the second parameter (the symbol that specifies the type of formatting):

Table 7-1: Predefined Numeric Formats

Format Name

Description

General Number, G, or g

Displays number with no thousand separator.

Currency, C, or c

Displays number with thousand separator, if appropriate; displays two digits to the right of the decimal separator. Output is based on system locale settings.

Fixed, F, or f

Displays at least one digit to the left and two digits to the right of the decimal separator.

Standard, N, or n

Displays number with thousand separator, at least one digit to the left and two digits to the right of the decimal separator.

Percent, P, or p

Displays number multiplied by 100 with a percent sign (%) appended to the right. Always displays two digits to the right of the decimal separator.

Scientific, E, or e

Uses standard scientific notation.

D or d

Displays number as a string that contains the value of the number in decimal (base 10) format. This option is supported for integral types (Byte, Short, Integer, Long) only.

X or x

Displays number as a string that contains the value of the number in hexadecimal (base 16) format. This option is supported for integral types (Byte, Short, Integer, Long) only.

Yes/No

Displays No if number is 0; otherwise, displays Yes.

True/False

Displays False if number is 0; otherwise, displays True.

On/Off

Displays Off if number is 0; otherwise, displays On.

Setting the Text property of the StatusBar control to "Calculation successful" is done outside of the inner If. Then..Else statement because it is independent of that statement. The output is the same whether the residency check box is checked or not.

Was this article helpful?

0 0

Post a comment