From 143e708681482ef4b8dd13280965ced42cd1e5c6 Mon Sep 17 00:00:00 2001 From: Mark J Price Date: Sun, 18 Sep 2022 11:16:18 +0100 Subject: [PATCH] Initial commit --- .../WritingFunctions/Program.Functions.cs | 10 +++---- vs4win/Chapter04/WritingFunctions/Program.cs | 4 +-- vs4win/Chapter05/PeopleApp/Program.cs | 26 ++++++++++++++++--- vs4win/Chapter07/Chapter07.sln | 12 ++++++--- vs4win/Chapter07/SourceLinks/Program.cs | 3 +++ .../Chapter07/SourceLinks/SourceLinks.csproj | 10 +++++++ .../WritingFunctions/Program.Functions.cs | 10 +++---- vscode/Chapter04/WritingFunctions/Program.cs | 4 +-- vscode/Chapter05/PeopleApp/Program.cs | 26 ++++++++++++++++--- vscode/Chapter07/Chapter07.code-workspace | 3 +++ vscode/Chapter07/SourceLinks/Program.cs | 3 +++ .../Chapter07/SourceLinks/SourceLinks.csproj | 10 +++++++ 12 files changed, 98 insertions(+), 23 deletions(-) create mode 100644 vs4win/Chapter07/SourceLinks/Program.cs create mode 100644 vs4win/Chapter07/SourceLinks/SourceLinks.csproj create mode 100644 vscode/Chapter07/SourceLinks/Program.cs create mode 100644 vscode/Chapter07/SourceLinks/SourceLinks.csproj diff --git a/vs4win/Chapter04/WritingFunctions/Program.Functions.cs b/vs4win/Chapter04/WritingFunctions/Program.Functions.cs index 4b27129..dfcfc02 100644 --- a/vs4win/Chapter04/WritingFunctions/Program.Functions.cs +++ b/vs4win/Chapter04/WritingFunctions/Program.Functions.cs @@ -67,7 +67,7 @@ case 11: // special cases for 11th to 13th case 12: case 13: - return $"{number}th"; + return $"{number:N0}th"; default: int lastDigit = number % 10; @@ -79,13 +79,13 @@ _ => "th" }; - return $"{number}{suffix}"; + return $"{number:N0}{suffix}"; } } static void RunCardinalToOrdinal() { - for (int number = 1; number <= 1030; number++) + for (int number = 1; number <= 1500; number++) { Write($"{CardinalToOrdinal(number)} "); } @@ -115,7 +115,7 @@ static void RunFactorial() { - for (int i = -2; i <= 14; i++) + for (int i = -2; i <= 15; i++) { try { @@ -127,7 +127,7 @@ } catch (Exception ex) { - WriteLine($"{ex.GetType()}: {ex.Message}."); + WriteLine($"{i}! throws {ex.GetType()}: {ex.Message}"); } } } diff --git a/vs4win/Chapter04/WritingFunctions/Program.cs b/vs4win/Chapter04/WritingFunctions/Program.cs index aad506b..c2e45e2 100644 --- a/vs4win/Chapter04/WritingFunctions/Program.cs +++ b/vs4win/Chapter04/WritingFunctions/Program.cs @@ -11,9 +11,9 @@ decimal taxToPay = CalculateTax(amount: 149, twoLetterRegionCode: "FR"); WriteLine($"You must pay {taxToPay} in tax."); */ -RunCardinalToOrdinal(); +//RunCardinalToOrdinal(); -//RunFactorial(); +RunFactorial(); //RunFibImperative(); diff --git a/vs4win/Chapter05/PeopleApp/Program.cs b/vs4win/Chapter05/PeopleApp/Program.cs index 37b75ae..f80431d 100644 --- a/vs4win/Chapter05/PeopleApp/Program.cs +++ b/vs4win/Chapter05/PeopleApp/Program.cs @@ -158,8 +158,19 @@ WriteLine(sam.Age); sam.FavoriteIceCream = "Chocolate Fudge"; WriteLine($"Sam's favorite ice-cream flavor is {sam.FavoriteIceCream}."); -sam.FavoritePrimaryColor = "Red"; -WriteLine($"Sam's favorite primary color is {sam.FavoritePrimaryColor}."); + +string color = "Black"; + +try +{ + sam.FavoritePrimaryColor = color; + WriteLine($"Sam's favorite primary color is {sam.FavoritePrimaryColor}."); +} +catch (Exception ex) +{ + WriteLine("Tried to set {0} to '{1}': {2}", + nameof(sam.FavoritePrimaryColor), color, ex.Message); +} /* Book book = new() @@ -237,7 +248,16 @@ for (int i = 0; i < lamech.Children.Count; i++) // Implementing functionality using local functions -WriteLine($"5! is {Person.Factorial(5)}"); +int number = -1; // change to -1 to make the exception handling code execute + +try +{ + WriteLine($"{number}! is {Person.Factorial(number)}"); +} +catch (Exception ex) +{ + WriteLine($"{ex.GetType()} says: {ex.Message} number was {number}."); +} // Pattern matching with objects diff --git a/vs4win/Chapter07/Chapter07.sln b/vs4win/Chapter07/Chapter07.sln index bd61759..72abad1 100644 --- a/vs4win/Chapter07/Chapter07.sln +++ b/vs4win/Chapter07/Chapter07.sln @@ -3,14 +3,16 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.2.32210.308 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssembliesAndNamespaces", "AssembliesAndNamespaces\AssembliesAndNamespaces.csproj", "{4FD636C5-91F6-46AC-97B7-2396F11A31A8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AssembliesAndNamespaces", "AssembliesAndNamespaces\AssembliesAndNamespaces.csproj", "{4FD636C5-91F6-46AC-97B7-2396F11A31A8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharedLibrary", "SharedLibrary\SharedLibrary.csproj", "{EDEB4531-EE20-440B-BB41-84CDA5715BA7}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharedLibrary", "SharedLibrary\SharedLibrary.csproj", "{EDEB4531-EE20-440B-BB41-84CDA5715BA7}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetEverywhere", "DotNetEverywhere\DotNetEverywhere.csproj", "{7509E104-A3AE-4AEB-9656-D83A15D5CD70}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetEverywhere", "DotNetEverywhere\DotNetEverywhere.csproj", "{7509E104-A3AE-4AEB-9656-D83A15D5CD70}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ControlSDK", "ControlSDK\ControlSDK.csproj", "{76808C33-E138-4170-9621-122C1993B36E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SourceLinks", "SourceLinks\SourceLinks.csproj", "{FA7C1721-0B08-42CA-A075-66FFEF57B9A5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {76808C33-E138-4170-9621-122C1993B36E}.Debug|Any CPU.Build.0 = Debug|Any CPU {76808C33-E138-4170-9621-122C1993B36E}.Release|Any CPU.ActiveCfg = Release|Any CPU {76808C33-E138-4170-9621-122C1993B36E}.Release|Any CPU.Build.0 = Release|Any CPU + {FA7C1721-0B08-42CA-A075-66FFEF57B9A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FA7C1721-0B08-42CA-A075-66FFEF57B9A5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FA7C1721-0B08-42CA-A075-66FFEF57B9A5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FA7C1721-0B08-42CA-A075-66FFEF57B9A5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/vs4win/Chapter07/SourceLinks/Program.cs b/vs4win/Chapter07/SourceLinks/Program.cs new file mode 100644 index 0000000..8593f3d --- /dev/null +++ b/vs4win/Chapter07/SourceLinks/Program.cs @@ -0,0 +1,3 @@ +string name = "Timothée Chalamet"; +int length = name.Count(); +Console.WriteLine($"{name} has {length} characters."); \ No newline at end of file diff --git a/vs4win/Chapter07/SourceLinks/SourceLinks.csproj b/vs4win/Chapter07/SourceLinks/SourceLinks.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/vs4win/Chapter07/SourceLinks/SourceLinks.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/vscode/Chapter04/WritingFunctions/Program.Functions.cs b/vscode/Chapter04/WritingFunctions/Program.Functions.cs index 4b27129..dfcfc02 100644 --- a/vscode/Chapter04/WritingFunctions/Program.Functions.cs +++ b/vscode/Chapter04/WritingFunctions/Program.Functions.cs @@ -67,7 +67,7 @@ case 11: // special cases for 11th to 13th case 12: case 13: - return $"{number}th"; + return $"{number:N0}th"; default: int lastDigit = number % 10; @@ -79,13 +79,13 @@ _ => "th" }; - return $"{number}{suffix}"; + return $"{number:N0}{suffix}"; } } static void RunCardinalToOrdinal() { - for (int number = 1; number <= 1030; number++) + for (int number = 1; number <= 1500; number++) { Write($"{CardinalToOrdinal(number)} "); } @@ -115,7 +115,7 @@ static void RunFactorial() { - for (int i = -2; i <= 14; i++) + for (int i = -2; i <= 15; i++) { try { @@ -127,7 +127,7 @@ } catch (Exception ex) { - WriteLine($"{ex.GetType()}: {ex.Message}."); + WriteLine($"{i}! throws {ex.GetType()}: {ex.Message}"); } } } diff --git a/vscode/Chapter04/WritingFunctions/Program.cs b/vscode/Chapter04/WritingFunctions/Program.cs index aad506b..c2e45e2 100644 --- a/vscode/Chapter04/WritingFunctions/Program.cs +++ b/vscode/Chapter04/WritingFunctions/Program.cs @@ -11,9 +11,9 @@ decimal taxToPay = CalculateTax(amount: 149, twoLetterRegionCode: "FR"); WriteLine($"You must pay {taxToPay} in tax."); */ -RunCardinalToOrdinal(); +//RunCardinalToOrdinal(); -//RunFactorial(); +RunFactorial(); //RunFibImperative(); diff --git a/vscode/Chapter05/PeopleApp/Program.cs b/vscode/Chapter05/PeopleApp/Program.cs index 37b75ae..f80431d 100644 --- a/vscode/Chapter05/PeopleApp/Program.cs +++ b/vscode/Chapter05/PeopleApp/Program.cs @@ -158,8 +158,19 @@ WriteLine(sam.Age); sam.FavoriteIceCream = "Chocolate Fudge"; WriteLine($"Sam's favorite ice-cream flavor is {sam.FavoriteIceCream}."); -sam.FavoritePrimaryColor = "Red"; -WriteLine($"Sam's favorite primary color is {sam.FavoritePrimaryColor}."); + +string color = "Black"; + +try +{ + sam.FavoritePrimaryColor = color; + WriteLine($"Sam's favorite primary color is {sam.FavoritePrimaryColor}."); +} +catch (Exception ex) +{ + WriteLine("Tried to set {0} to '{1}': {2}", + nameof(sam.FavoritePrimaryColor), color, ex.Message); +} /* Book book = new() @@ -237,7 +248,16 @@ for (int i = 0; i < lamech.Children.Count; i++) // Implementing functionality using local functions -WriteLine($"5! is {Person.Factorial(5)}"); +int number = -1; // change to -1 to make the exception handling code execute + +try +{ + WriteLine($"{number}! is {Person.Factorial(number)}"); +} +catch (Exception ex) +{ + WriteLine($"{ex.GetType()} says: {ex.Message} number was {number}."); +} // Pattern matching with objects diff --git a/vscode/Chapter07/Chapter07.code-workspace b/vscode/Chapter07/Chapter07.code-workspace index 673f955..628d5d7 100644 --- a/vscode/Chapter07/Chapter07.code-workspace +++ b/vscode/Chapter07/Chapter07.code-workspace @@ -11,6 +11,9 @@ }, { "path": "DotNetEverywhere" + }, + { + "path": "SourceLinks" } ] } \ No newline at end of file diff --git a/vscode/Chapter07/SourceLinks/Program.cs b/vscode/Chapter07/SourceLinks/Program.cs new file mode 100644 index 0000000..8593f3d --- /dev/null +++ b/vscode/Chapter07/SourceLinks/Program.cs @@ -0,0 +1,3 @@ +string name = "Timothée Chalamet"; +int length = name.Count(); +Console.WriteLine($"{name} has {length} characters."); \ No newline at end of file diff --git a/vscode/Chapter07/SourceLinks/SourceLinks.csproj b/vscode/Chapter07/SourceLinks/SourceLinks.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/vscode/Chapter07/SourceLinks/SourceLinks.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + +