☰ Year plan · Communication Technology

The converter in VB.NET: window, code and tests

Week 6 · Unit 1 Topic 2: a number converter project with VB.NET: window, controls, properties and code · 3 lessons
2

Reading the converter code part by part

ICT Secondary 3 Term 1, Unit 1, Topic 2: pp. 27–36
GoalRead the click handler of the converter in five parts and say what each part does.
New words
event · something that happens to a control, such as a clickevent handler · the block of code that runs when its event happensvariable · a named place in memory that holds a valueDim · the word that declares a variable: it gives the name and the type
Explanation

The code lives in the click event of the button. An event is something that happens, here a click; the event handler is the block of code that runs when it does. Our handler has five parts. Part 1 is the first line, which says that the block runs when ButtonConvert is clicked. Part 2 declares four variables with Dim: the typed text, the base to read, the base to write and the number. Part 3 looks at the three From radio buttons and sets the base to read: 10, 16 or 2. Part 4 does the same for To. Part 5 uses two ready functions: one reads the text in the From base and gives a number, the other writes that number in the To base, and a message box shows it. A number has no base of its own; it is just an amount, and the two bases only say how it is written going in and coming out.

PartWhat it does
1the line that says which control and event start the block
2declares the typed text, the base to read, the base to write and the number
3sets the base to read from the From radio buttons
4sets the base to write from the To radio buttons
5reads the text, writes the number, shows the answer
Worked examples
Vb
1' Part 1: this block runs when the button is clicked
2Private Sub ButtonConvert_Click(sender As Object, e As EventArgs) Handles ButtonConvert.Click
3    ' Part 2: four variables
4    Dim typed As String = TextNumber.Text
5    Dim fromBase As Integer
6    Dim toBase As Integer
7    Dim number As Integer
8
9    ' Part 3: which system is the typed number in?
10    If RadioFromDec.Checked Then
11        fromBase = 10
12    ElseIf RadioFromHex.Checked Then
13        fromBase = 16
14    Else
15        fromBase = 2
16    End If
17
18    ' Part 4: which system do we want?
19    If RadioToDec.Checked Then
20        toBase = 10
21    ElseIf RadioToHex.Checked Then
22        toBase = 16
23    Else
24        toBase = 2
25    End If
26
27    ' Part 5: read, write, show
28    number = Convert.ToInt32(typed, fromBase)
29    MessageBox.Show(Convert.ToString(number, toBase))
30End Sub
This is the whole handler. Read it in the five parts. We call the fourth variable number: it holds the amount, not a base.
After parttypedfromBasetoBasenumber
21101000
31101200
411012100
5110121013
Trace it on paper. The user types 1101, ticks Binary in From and Decimal in To. In Part 3 the first two tests fail, so the last Else sets 2. The table shows the variables after each part. The message box shows 13.
TypedFromToMessage
1010nothing tickedDecimal10
1010Decimalnothing ticked1111110010
A trap: the last Else of each part also catches the case where nothing is ticked, so it silently chooses base 2. These two runs come from the real code.
Class activity

Trace the handler on paper for the typed text 75 with Decimal ticked in From and Binary ticked in To. Write the values of fromBase, toBase and number after Parts 3, 4 and 5, and the text in the message box.

Practice
1
How many variables does the handler declare, and what does each hold?
2
What value does Part 4 give toBase when RadioToHex is ticked?
3
Which function runs first, ToInt32 or ToString, and why?
4
Why is the fourth variable called number and not fromBase?