Why do we do this...
Section 4.0
The InputBox function
This pops up - like a message box - but it asks you for data. You can supply a message to assist the user - called a prompt.
Example:
Dim s as string
s = inputBox("Type your name")
They look like:
A pop-up input box
Basically, you type any text into the box, and this ends up in the variable we use on the left of =.
In our programs so far, we have set variables by e.g:
Dim salary, deductions as integer
salary = 23000
This is termed 'hard-wiring'. The programmer - not the user - has to amend the program to change the value. BUT the use of input boxes is much better:
salary=CInt(inputBox("How much do you earn"))
deductions=CInt(inputBox("How much are your deductions"))
finalPay = salary-deductions
Now, the program is not fixed to particular values.
Why has CInt() been used above? This is because input boxes get strings of text (possibly alphabetic) from the user.
If we need to store the text in a numeric variable(i.e double or integer) we must ask VB to 'convert to Int' or Convert to Dbl' for us.
Here are some examples, which you can base your programs on:
Dim age as integer
Dim heightInMetres as double
Dim name as string
'now ask the user for some data
age = CInt(InputBox("How old are you:")) 'NB nested brackets ( ( ) )
heightInMetres = CDbl(InputBox("How tall are you (in metres please):"))
name = InputBox("What is your name:") 'name is string - no conversion needed
Section 4.1
Problems - inputBox (main tutorial)
Re-do the 'Problems with + - * /' from the Variables and Calculations chapter, but input initial values from the user where appropriate, rather than setting them with assignment =. Produce meaningful output by using & to join a message to the answer.
Section 4.2
Type conversion in General
What if your height (1.94 metres) got accidentally truncated to a whole number (1). Not good!
In programming, we need to be aware about the types of variables, and how they get converted. This often happens automatically. The rules are slightly different, depending on your language.
If we have
dim i as integer
dim d as double
dim s as string
i = 3 'ok 3 is whole number
d = 4.9 'ok
i = 3.9 ' hmm... 3.9 is not whole - problem - VB forbids this
i = d 'not allowed to put a double into an integer
d = i 'ok - no data lost
So, VB will not let us store a decimal point item into an integer - because the decimal places are removed - we have
lost some information. Normally we don't need to do this, but sometimes we do. We need to tell VB that we know what we are doing, by using:
i = CInt(d) ' convert double to int before assigning
and sometimes we need to convert a number to a string before displaying it - because message boxes need strings:
MessageBox.Show(CStr(d))
Section 4.3
Constants
What if you saw the number 2.54 in a kitchen planner program.
Would you know what it meant? Probably not. In fact, it is the number of cm in one inch. The program
would be clearer if the programmer had named it - e.g. cmPerInch. Constants make programs easier to understand, hence modify.
Some numbers in a program do not change as it runs. For example, tax rate - this may need altering once every year, when a budget happens. There are also maths constants - like pi - which is fixed for all time.
In VB, we can express this non-changing by const, as in:
Const weeksInYear As Integer= 52
dim mortgageYears As integer
mortgageYears = 12
MessageBox.Show(mortgageYears * weeksInYear )
We could have written:
MessageBox.Show( mortgageYears * 52)
but:
Section 4.4
Problems - constants
1. Pi is 3.141 approx. We have a pizza of 12" diameter. If 3 people share it, how many sq inches do they get each? Use a constant in your solution.
Key Points
Constants make programs easier to read and modify.
CInt(...) CDbl(...) - If you get a number from an input box or a text box, put:
dim n as integer
n = CInt(inputBox("type a number"))
n= CInt(textBox1.text)
Dim d As Double
d = CDbl(inputBox("type a number"))
d = CDbl(textBox1.text)
|