From 28ef9a8c4f197b4ad08d025918444efdf1cecea2 Mon Sep 17 00:00:00 2001 From: Hion-V Date: Thu, 26 Jan 2023 12:14:01 +0100 Subject: [PATCH] Resolved issue described in https://github.com/MeesterReneOrg/oop-csharp-Hion-V/issues/1#issue-1524609342 --- .../classes/Model.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/OOP-Opdracht-CSharp-Alfa-2022/OOP-Opdracht-CSharp-Alfa-2022/classes/Model.cs b/OOP-Opdracht-CSharp-Alfa-2022/OOP-Opdracht-CSharp-Alfa-2022/classes/Model.cs index 73acea3..3cf5456 100644 --- a/OOP-Opdracht-CSharp-Alfa-2022/OOP-Opdracht-CSharp-Alfa-2022/classes/Model.cs +++ b/OOP-Opdracht-CSharp-Alfa-2022/OOP-Opdracht-CSharp-Alfa-2022/classes/Model.cs @@ -43,12 +43,22 @@ namespace OOP_Opdracht_CSharp_Alfa_2022.classes // Get KeyValuePair by id, returns [-1, "Invalid Record"] when out of bounds public KeyValuePair find(int id = 1) { - KeyValuePair record = new KeyValuePair(-1, "Invalid Record"); - if (this.table_data.ContainsKey(id)) + // Initialize KeyValuePair as null + KeyValuePair record; + // Populate using record from dictionary + record = new KeyValuePair(id, this.table_data.GetValueOrDefault(id)); + // Handle default value + if(record.Value == default) { - record = new KeyValuePair(id, this.table_data.GetValueOrDefault(id)); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"Attempting to request invalid record with id: {id}"); + Console.ResetColor(); } - this.record = record; + else + { + this.record = record; + } + // Return record return record; } }