☰ خطة العام · تكنولوجيا المعلومات والاتصالات (بالإنجليزية)

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

الأسبوع 6 · الوحدة ١ الموضوع الثاني: مشروع محوّل الأعداد بلغة VB.NET: النافذة وأدوات التحكم والخصائص والكود · 3 دروس
3

Ready conversion functions: testing with known answers

ICT Secondary 3 Term 1, Unit 1, Topic 2: pp. 27–36
GoalUse Convert.ToInt32 and Convert.ToString, test the converter with answers you can check by hand, and name the inputs that make it stop.
New words
ready function · a function that comes with the language, so we do not write the steps ourselvestest case · an input together with the answer we expectexception · an error that stops the program when something unexpected happensTry … Catch · a block that tries some code and, if an exception happens, runs a safe message instead
Explanation

Two ready functions do the whole job. Convert.ToInt32(text, base) reads text written in base 2, 10 or 16 and gives a whole number. Convert.ToString(number, base) writes a number in a base. Hexadecimal comes out in lower case, so 255 becomes ff; adding .ToUpper() gives FF, the way we wrote it by hand in Weeks 4 and 5. How do we know that the program is right? We test it with known answers, numbers that we converted by hand: a test case is an input with the answer we expect. Then we also test bad inputs: an empty box, a wrong symbol such as 2 in a binary number, a minus sign in a binary or hexadecimal number, or a decimal number above 2,147,483,647. Each of these stops the program with an exception. Wrapping the two lines in Try … Catch lets the program show a polite message instead.

FunctionExampleGives
Convert.ToInt32(text, base)Convert.ToInt32("3C", 16)60 (a number)
Convert.ToString(number, base)Convert.ToString(60, 16)3c (text, hexadecimal in lower case)
Worked examples
TypedFromToMessageWith .ToUpper()
111100BinaryDecimal6060
60DecimalHexadecimal3c3C
3CHexadecimalBinary111100111100
255DecimalHexadecimalffFF
Four tests with answers that we can check by hand. They belong together: 60 is 111100 in binary and 3C in hexadecimal.
TypedFromWhat happens
(empty box)anythe program stops
12Binarythe program stops: 2 is not a binary symbol
-101Binarythe program stops: a minus sign is accepted only in decimal
3000000000Decimalthe program stops: too big for a whole number of this kind
FFFFFFFFHexadecimalno error, but the message is -1, a strange result
These bad inputs were run on the plain handler.
Vb
1Try
2    number = Convert.ToInt32(typed, fromBase)
3    MessageBox.Show(Convert.ToString(number, toBase).ToUpper())
4Catch ex As Exception
5    MessageBox.Show("Please type a valid number for the system you chose.")
6End Try
Output
D
Please type a valid number for the system you chose.
The same two lines inside Try … Catch, with capitals. The first run types 1101 with Binary to Hexadecimal; the second types 12 with Binary to Decimal.
Class activity

Make a test table on paper with five rows for the converter. Each row has a typed text, the From and To systems and the answer you expect from hand conversion. At least one row must be a bad input; say which message the Try … Catch version should show for it.

Practice
1
What does Convert.ToString(255, 16) give?
2
The plain handler gets the text 12 with Binary ticked in From. What happens?
3
Why does a test table use answers worked out by hand?
4
Name two inputs that stop the plain converter.