الأسبوع 6 · الوحدة ١ الموضوع الثاني: مشروع محوّل الأعداد بلغة VB.NET: النافذة وأدوات التحكم والخصائص والكود · 3 دروس
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.
Part
What it does
1
the line that says which control and event start the block
2
declares the typed text, the base to read, the base to write and the number
3
sets the base to read from the From radio buttons
4
sets the base to write from the To radio buttons
5
reads 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
89 ' Part 3: which system is the typed number in?
10 If RadioFromDec.Checked Then
11 fromBase = 1012 ElseIf RadioFromHex.Checked Then
13 fromBase = 1614 Else
15 fromBase = 216 End If
1718 ' Part 4: which system do we want?
19 If RadioToDec.Checked Then
20 toBase = 1021 ElseIf RadioToHex.Checked Then
22 toBase = 1623 Else
24 toBase = 225 End If
2627 ' 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 part
typed
fromBase
toBase
number
2
1101
0
0
0
3
1101
2
0
0
4
1101
2
10
0
5
1101
2
10
13
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.
Typed
From
To
Message
1010
nothing ticked
Decimal
10
1010
Decimal
nothing ticked
1111110010
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?
Four: the typed text, the base to read, the base to write and the number.
2
What value does Part 4 give toBase when RadioToHex is ticked?
16.
3
Which function runs first, ToInt32 or ToString, and why?
ToInt32 first: it turns the typed text into a number, and ToString then writes that number in the other base.
4
Why is the fourth variable called number and not fromBase?
It holds an amount, not a base, and a clear name stops readers mixing the two up.