On completion of this chapter you will know
·
What is a String data type
·
How to get String data from the user and print it
Up to now we have been using numeric data of type Single, Currency and Integer in order to demonstrate how to
process a simple payroll application. We have, however, been ignoring the most
important part of a payroll application – the name of the person being paid.
To write a person’s name we have to use ordinary text
instead of numbers and thus the data types of Single, Currency and Integer
that we have been using up to now are no good to us. In order to handle text we
must use the data type String.
Listing 8‑1 below shows one
more version of Sub Pay this time
with String data added.
Listing 8‑1
|
1 |
Sub Pay() |
|
2 |
Dim sngHours As Single |
|
3 |
Dim curRate As Currency |
|
4 |
Dim curGross As Currency |
|
5 |
Dim curTax As Currency |
|
6 |
Dim curNett
As Currency |
|
7 |
Dim curSuper As Currency |
|
8 |
Dim intSuperCode
As Integer |
|
9 |
Dim strName
As String |
|
10 |
Dim strSurname
As String |
|
11 |
Dim strFullName
As String |
|
12 |
strName
= InputBox("Enter first name of employee") |
|
13 |
strSurname
= InputBox("Enter surname of employee") |
|
14 |
sngHours =
InputBox("Enter Hours worked") |
|
15 |
curRate =
InputBox("Enter Rate") |
|
16 |
intSuperCode
= InputBox("Enter superannuation code") |
|
17 |
strFullName
= strName & " " & strSurname |
|
18 |
curGross = calculateGross(sngHours, curRate) |
|
19 |
curTax = calculateTax(curGross) |
|
20 |
curSuper = calculateSuper(intSuperCode,
curGross) |
|
21 |
curNett
= calculateNett(curGross, curTax, curSuper) |
|
22 |
showdata
strFullName, sngHours, curRate, curGross, curTax,
curSuper, curNett |
|
23 |
End Sub |
|
24 |
|
|
25 |
Sub showdata(name As String, hours As
Single, rate As Currency, gross As Currency, tax As Currency, super As
Currency, nett As Currency) |
|
26 |
Debug.Print "Employee
name", name |
|
27 |
Debug.Print "Hours
", hours |
|
28 |
Debug.Print "Rate
", rate |
|
29 |
Debug.Print "Gross
", gross |
|
30 |
Debug.Print "Tax",
tax |
|
31 |
Debug.Print
"Super", super |
|
32 |
Debug.Print "Nett", nett |
|
33 |
End Sub |
At lines 9, 10 and 11 we have declared three String
variables, strName, strSurname
and strFullName. At lines 12 and
13 we get values form the variables strName and strSurname using the InputBox function. At line 17, at the start of the processing
section, we update the value of strFullName using
the operator “&”. This operator adds two strings together. Thus the line of
code
strWorker = “James” &
“Graham” will result in the value “JamesGraham”
to be stored in the variable strWorker. If we
wish to have a space separating the name and surname we must alter our code to
strWorker = “James” & “
“ & “Graham”
For this reason, at line 17, we have
strFullName = strName & “ “ & strSurname
where the name and the
surname will be separated by a space. At line 22 the call to showData
includes the variable strFullName and at line 25,
where showData is defined, an extra argument has been added to its
list of arguments. The new String
argument, name has been placed before the other arguments.
Copy the
code of Listing 8‑1into your programme area. Since none of the functions for calculating
the various payroll items are included here you must copy those from Listing 7‑1.
The same
test data will do as that for Part 6, but check each time that the employee’s
name is printed along with the rest of the other data items.
Write a
programme that accepts two string data items from the user, a person’s name and
surname. The two data items are printed
out in one string item with labels. If the
user entered the items “John” and “Smith” then the output of the programme
would be “The name is John and the surname is Smith”
Modify the
programme that you have created for Assignment Part 6 so that two string items
are processed: Name of the customer and Description of item purchased. The name of the customer should be entered
only once while the description of item purchased has to be entered for each
item. No processing occurs on those two
string items but their values should be printed out along with the subtotal
etc.