Which code segment should you add at line 19?

You are developing an application that includes the following code segment. (Line numbers are included for
reference only.)
01 using System;
02 class MainClass
03 {
04 public static void Main(string[] args)
05 {
06 bool bValidInteger = false;
07 int value = 0;
08 do
09 {
10 Console.WriteLine(“Enter an integer”);
11 bValidInteger = GetValidInteger(ref value);
12 } while (!bValidInteger);
13 Console.WriteLine(“You entered a valid integer, ” + value);
14 }
15 public static bool GetValidInteger(ref int val)
16 {
17 string sLine = Console.ReadLine();
18 int number;
19
20 {
21 return false;
22 }
23 else
24 {
25 val = number;
26 return true;
27 }
28 }
29 }
You need to ensure that the application accepts only integer input and prompts the user each time non-integer
input is entered. Which code segment should you add at line 19?

You are developing an application that includes the following code segment. (Line numbers are included for
reference only.)
01 using System;
02 class MainClass
03 {
04 public static void Main(string[] args)
05 {
06 bool bValidInteger = false;
07 int value = 0;
08 do
09 {
10 Console.WriteLine(“Enter an integer”);
11 bValidInteger = GetValidInteger(ref value);
12 } while (!bValidInteger);
13 Console.WriteLine(“You entered a valid integer, ” + value);
14 }
15 public static bool GetValidInteger(ref int val)
16 {
17 string sLine = Console.ReadLine();
18 int number;
19
20 {
21 return false;
22 }
23 else
24 {
25 val = number;
26 return true;
27 }
28 }
29 }
You need to ensure that the application accepts only integer input and prompts the user each time non-integer
input is entered. Which code segment should you add at line 19?

A.
if (!int.TryParse(sLine, out number))

B.
if ((number = Int32.Parse(sLine)) == Single.NaN)

C.
if ((number = int.Parse (sLine)) > Int32.MaxValue)

D.
if (Int32.TryParse(sLine, out number))

Explanation:
B and C will throw exception when user enters non-integer value. D is exactly the opposite what we want to
achieve.
Int32.TryParse – Converts the string representation of a number to its 32-bit signed integer equivalent. A
return value indicates whether the conversion succeeded. http://msdn.microsoft.com/en-us/library/
f02979c7.aspx



Leave a Reply 0

Your email address will not be published. Required fields are marked *