You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: learn-pr/wwl-language/csharp-convert-cast/includes/3-exercise-tryparse.md
+99-24Lines changed: 99 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,57 +40,132 @@ Methods can return a value or return "void", meaning they return no value. Metho
40
40
1. Delete or use the line comment operator `//` to comment out all of the code from the previous exercises.
41
41
42
42
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
+
stringvalue="102";
46
+
intresult=0;
47
+
if (int.TryParse(value, outresult))
48
+
{
49
+
Console.WriteLine($"Measurement: {result}");
50
+
}
51
+
else
52
+
{
53
+
Console.WriteLine("Unable to report the measurement.");
54
+
}
55
+
```
44
56
45
57
1. Examinethislineofcode:
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:
The `int.TryParse()` methodreturns `true` ifitsuccessfullyconvertedthe `string` variable `value` intoan `int`; otherwise, itreturns `false`. So, surroundthestatementinan `if` statement, andthenperformthedecisionlogic, accordingly.
66
+
67
+
Theconvertedvalueisstoredinthe `int` variable `result`. The `int` variable `result` isdeclaredandinitializedbeforethislineofcode, soitshouldbeaccessibleboth*inside*thecodeblocksthatbelongtothe `if` and `else` statements, aswellas*outside*ofthem.
68
+
69
+
The `out` keywordinstructsthecompilerthatthe `TryParse()` methodwon't return a value the traditional way only (as a return value), but also will communicate an output through this two-way parameter.
```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."); }
>Ifyouseeamessagesaying"Couldn't find a project to run", ensurethattheTerminalcommandpromptdisplaystheexpectedTestProjectfolderlocation. Forexample: `C:\Users\someuser\Desktop\csharpprojects\TestProject>`
Since `result` isdefinedoutsideofthe `if` statement, `result` canbeaccessedlaterinyourcodeoutsideofthecodeblocks. Sothen `result` canbecheckedfor 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.
Since `result` isdefinedoutsideofthe `if` statement, `result` canbeaccessedlaterinyourcodeoutsideofthecodeblocks. Sothen `result` canbecheckedfor 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.
94
169
95
170
## Recap
96
171
@@ -99,4 +174,4 @@ The `TryParse()` method is a valuable tool. Here are few quick ideas to remember
99
174
- Use `TryParse()` when converting a string into a numeric data type.
0 commit comments