Skip to content

Commit 0b92b97

Browse files
authored
Update 3-exercise-tryparse.md
1 parent 828d30d commit 0b92b97

File tree

1 file changed

+99
-24
lines changed

1 file changed

+99
-24
lines changed

learn-pr/wwl-language/csharp-convert-cast/includes/3-exercise-tryparse.md

Lines changed: 99 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,57 +40,132 @@ Methods can return a value or return "void", meaning they return no value. Metho
4040
1. Delete or use the line comment operator `//` to comment out all of the code from the previous exercises.
4141

4242
1. Update your code in the Visual Studio Code Editor as follows:
43-
```csharp string value = "102"; int result = 0; if (int.TryParse(value, out result)) { Console.WriteLine($"Measurement: {result}"); } else { Console.WriteLine("Unable to report the measurement."); } ```
43+
44+
```csharp
45+
string value = "102";
46+
int result = 0;
47+
if (int.TryParse(value, out result))
48+
{
49+
Console.WriteLine($"Measurement: {result}");
50+
}
51+
else
52+
{
53+
Console.WriteLine("Unable to report the measurement.");
54+
}
55+
```
4456

4557
1. Examine this line of code:
46-
``` if (int.TryParse(value, out result)) ```
47-
When calling a method with an `out` parameter, you must use the keyword `out` before the variable, which holds the value. The `out` parameter is assigned to the `result` variable in the code `(int.TryParse(value,`**` out result`**`)`. You can then use the value the `out` parameter contains throughout the rest of your code using the variable `result`.
48-
The `int.TryParse()` method returns `true` if it successfully converted the `string` variable `value` into an `int`; otherwise, it returns `false`. So, surround the statement in an `if` statement, and then perform the decision logic, accordingly.
49-
The converted value is stored in the `int` variable `result`. The `int` variable `result` is declared and initialized before this line of code, so it should be accessible both *inside* the code blocks that belong to the `if` and `else` statements, as well as *outside* of them.
50-
The `out` keyword instructs the compiler that the `TryParse()` method won't return a value the traditional way only (as a return value), but also will communicate an output through this two-way parameter.
51-
When you run the code, you should see the following output:
52-
```Output Measurement: 102 ```
58+
59+
```
60+
if (int.TryParse(value, out result))
61+
```
62+
63+
When calling a method with an `out` parameter, you must use the keyword `out` before the variable, which holds the value. The `out` parameter is assigned to the `result` variable in the code `(int.TryParse(value,`**` out result`**`)`. You can then use the value the `out` parameter contains throughout the rest of your code using the variable `result`.
64+
65+
The `int.TryParse()` method returns `true` if it successfully converted the `string` variable `value` into an `int`; otherwise, it returns `false`. So, surround the statement in an `if` statement, and then perform the decision logic, accordingly.
66+
67+
The converted value is stored in the `int` variable `result`. The `int` variable `result` is declared and initialized before this line of code, so it should be accessible both *inside* the code blocks that belong to the `if` and `else` statements, as well as *outside* of them.
68+
69+
The `out` keyword instructs the compiler that the `TryParse()` method won't return a value the traditional way only (as a return value), but also will communicate an output through this two-way parameter.
70+
71+
When you run the code, you should see the following output:
72+
73+
```Output
74+
Measurement: 102
75+
```
5376

5477
### Use the parsed `int` later in code
5578

5679
1. To demonstrate that the `result` variable that was declared earlier, is populated by the `out` parameter and is also usable later in your code, update your code in the Visual Studio Code Editor as follows:
57-
```csharp string value = "102"; int result = 0; if (int.TryParse(value, out result)) { Console.WriteLine($"Measurement: {result}"); } else { Console.WriteLine("Unable to report the measurement."); }
58-
Console.WriteLine($"Measurement (w/ offset): {50 + result}"); ```
80+
81+
```csharp
82+
string value = "102";
83+
int result = 0;
84+
if (int.TryParse(value, out result))
85+
{
86+
Console.WriteLine($"Measurement: {result}");
87+
}
88+
else
89+
{
90+
Console.WriteLine("Unable to report the measurement.");
91+
}
92+
93+
Console.WriteLine($"Measurement (w/ offset): {50 + result}");
94+
```
5995

6096
1. On the Visual Studio Code **File** menu, select **Save**.
61-
The Program.cs file must be saved before building or running the code.
97+
98+
The Program.cs file must be saved before building or running the code.
6299

63100
1. In the EXPLORER panel, to open a Terminal at your TestProject folder location, right-click **TestProject**, and then select **Open in Integrated Terminal**.
64-
A Terminal panel should open, and should include a command prompt showing that the Terminal is open to your TestProject folder location.
101+
102+
A Terminal panel should open, and should include a command prompt showing that the Terminal is open to your TestProject folder location.
65103

66104
1. At the Terminal command prompt, to run your code, type **dotnet run** and then press Enter.
67-
> [!NOTE] > If you see a message saying "Couldn't find a project to run", ensure that the Terminal command prompt displays the expected TestProject folder location. For example: `C:\Users\someuser\Desktop\csharpprojects\TestProject>`
68-
You should see the following output:
69-
```Output Measurement: 102 Measurement (w/ offset): 152 ```
105+
106+
> [!NOTE]
107+
> If you see a message saying "Couldn't find a project to run", ensure that the Terminal command prompt displays the expected TestProject folder location. For example: `C:\Users\someuser\Desktop\csharpprojects\TestProject>`
108+
109+
You should see the following output:
110+
111+
```Output
112+
Measurement: 102
113+
Measurement (w/ offset): 152
114+
```
70115

71116
1. Examine the last line of code in the previous sample, `Console.WriteLine($"Measurement (w/ offset): {50 + result}");`,
72-
Since the `result` variable is defined outside of the if statement, it can be accessed later in your code. */
117+
118+
Since the `result` variable is defined outside of the if statement, it can be accessed later in your code. */
73119

74120
### Modify the string variable to a value that can't be parsed
75121

76122
Lastly, look at the other scenario - where the `TryParse()` is intentionally given a bad value that can't be converted into an `int`.
77123

78124
1. Modify the first line of code, reinitialize the variable `value` to a different value.
79-
```csharp string value = "bad"; ```
125+
126+
```csharp
127+
string value = "bad";
128+
```
80129

81130
1. Also, modify the last line of code to ensure that the result is greater than 0 before showing the second message.
82-
```csharp if (result > 0) Console.WriteLine($"Measurement (w/ offset): {50 + result}"); ```
131+
132+
```csharp
133+
if (result > 0)
134+
Console.WriteLine($"Measurement (w/ offset): {50 + result}");
135+
```
83136

84137
1. The entire code example should now match the following code:
85-
```csharp string value = "bad"; int result = 0; if (int.TryParse(value, out result)) { Console.WriteLine($"Measurement: {result}"); } else { Console.WriteLine("Unable to report the measurement."); }
86-
if (result > 0) Console.WriteLine($"Measurement (w/ offset): {50 + result}"); ```
138+
139+
```csharp
140+
string value = "bad";
141+
int result = 0;
142+
if (int.TryParse(value, out result))
143+
{
144+
Console.WriteLine($"Measurement: {result}");
145+
}
146+
else
147+
{
148+
Console.WriteLine("Unable to report the measurement.");
149+
}
150+
151+
if (result > 0)
152+
Console.WriteLine($"Measurement (w/ offset): {50 + result}");
153+
```
87154

88155
1. Save your code file, and then use Visual Studio Code to run your code. You should get the following result:
89-
```Output Unable to report the measurement. ```
156+
157+
```Output
158+
Unable to report the measurement.
159+
```
90160

91161
1. Examine the last two lines of code added in the previous sample.
92-
```csharp if (result > 0) Console.WriteLine($"Measurement (w/ offset): {50 + result}"); ```
93-
Since `result` is defined outside of the `if` statement, `result` can be accessed later in your code outside of the code blocks. So then `result` can be checked for a value greater than zero before allowing `result` + offset to be written as output. Checking for a `result` value greater than zero avoids printing an offset value after the `Unable to report the measurement.` message.
162+
163+
```csharp
164+
if (result > 0)
165+
Console.WriteLine($"Measurement (w/ offset): {50 + result}");
166+
```
167+
168+
Since `result` is defined outside of the `if` statement, `result` can be accessed later in your code outside of the code blocks. So then `result` can be checked for a value greater than zero before allowing `result` + offset to be written as output. Checking for a `result` value greater than zero avoids printing an offset value after the `Unable to report the measurement.` message.
94169

95170
## Recap
96171

@@ -99,4 +174,4 @@ The `TryParse()` method is a valuable tool. Here are few quick ideas to remember
99174
- Use `TryParse()` when converting a string into a numeric data type.
100175
- `TryParse()` returns `true` if the conversion is successful, `false` if it's unsuccessful.
101176
- Out parameters provide a secondary means of a method returning a value. In this case, the out parameter returns the converted value.
102-
- Use the keyword `out` when passing in an argument to a method that has defined an out parameter.
177+
- Use the keyword `out` when passing in an argument to a method that has defined an out parameter.

0 commit comments

Comments
 (0)