Fixing future problematic code?

I am a very new C # programmer, less than ~ 24 actual hours of raw typing / programming. I would say about a week about reading, observing and trying to understand things.

Here is the source code for my first program:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Bugcheck { class Program { static void Main(string[] args) { // Initializing variables string bugcheckExit = "exit"; var message = ""; var afterMessage = "Type another bug check, or \"exit\" to quit:\n"; Console.BackgroundColor = ConsoleColor.Blue; Console.Clear(); // Write welcome message to screen Console.WriteLine("Type a bug check code (for example - 0xD1), or \"exit\" to quit:\n"); while (true) { // Take in user input string userInput = Console.ReadLine().ToLower(); // Check user input if (userInput == bugcheckExit) System.Environment.Exit(1); // Checking value of bug check if (userInput == "0xd1") message = "DRIVER_IRQL_NOT_LESS_OR_EQUAL\n" + "This indicates that a kernel-mode driver attempted to access pageable memory at\n" + "a process IRQL that was too high.\n"; else if (userInput == "0xa") message = "IRQL_NOT_LESS_OR_EQUAL\n" + "This indicates that Microsoft Windows or a kernel-mode driver accessed\n" + "paged memory at DISPATCH_LEVEL or above.\n"; else if (userInput == "0x1e") message = "KMODE_EXCEPTION_NOT_HANDLED\n" + "This indicates that a kernel-mode program generated an exception which the\n" + "error handler did not catch.\n"; else message = "Not a valid bug check, please try again.\n"; Console.WriteLine(message); Console.WriteLine(afterMessage); } } } } 

The program does exactly what I want. The user enters the error verification code that they receive, he discards the basic definition of MSDN-MSFT and asks you to either type another error verification code or exit to exit the program.

However, according to the video / reading materials, it is important to learn how to identify the code that will be a problem in the future. Well, at least from my point of view, the method in which I use to check the value of user input in relation to error checking is potentially not very good, because it will ultimately be too large and messy, given the number of error checks there .

So, I did some research and learned about dictionaries, neatly. I came up with this:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Dictionary { class Program { static void Main(string[] args) { string value = ""; Dictionary<string, string> openWith = new Dictionary<string, string> { { "0xa", "This indicates that Microsoft Windows or a kernel-mode driver accessed\n" + "paged memory at DISPATCH_LEVEL or above.\n" }, { "0xd1", "This indicates that a kernel-mode driver attempted to access pageable memory at\n" + "a process IRQL that was too high.\n" } }; if (openWith.TryGetValue("0xd1", out value)) { Console.WriteLine(value); } else { Console.WriteLine(" is not found"); } Console.ReadLine(); } } } 

Now the question for me all the time was β€œHow the hell am I implementing this in my current code?” And I still don't know what I'm ashamed to say. I tried searching and exploring the creation / management of new classes, and although it was a bit confusing, I got the basic idea of ​​this and wrote a quick program that does not ask for user input, but instead simply prints to the console

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GPU { class Program { static void Main(string[] args) { GPU myNewGPU = new GPU(); myNewGPU.Make = "nVidia"; myNewGPU.Model = "GTX 970"; myNewGPU.Year = 2014; myNewGPU.Color = "Black"; Console.WriteLine("{0} - {1} - {2}", myNewGPU.Make, myNewGPU.Model, myNewGPU.Color); Console.WriteLine("GPU value: {0:G}", myNewGPU.DetermineMarketValue()); Console.ReadLine(); } } class GPU { public string Make { get; set; } public string Model { get; set; } public int Year { get; set; } public string Color { get; set; } public double DetermineMarketValue() { double GPUValue = 100.0; if (this.Year > 2010) GPUValue = 400.0; else GPUValue = 100.0; return GPUValue; } } } 

So, I understand the basics of dictionaries and classes, but I still don't know how to implement / the best method in my current error checking program.

Any tips, tips? I tried to make my message to show that I try to figure it out myself as much as possible, because I know that StackOverflow frowns when people just try to get others to encode them, I'm just not sure where else to look / read to understand this, or at least take a step in the right direction.

+7
c #
source share
1 answer

Here is a very simple implementation of your Dictionary:

  static void Main(string[] args) { Dictionary<string, string> openWith = new Dictionary<string, string> { { "0xa", "This indicates that Microsoft Windows or a kernel-mode driver accessed\n" + "paged memory at DISPATCH_LEVEL or above.\n" }, { "0xd1", "This indicates that a kernel-mode driver attempted to access pageable memory at\n" + "a process IRQL that was too high.\n" } }; string userInput = ""; while ((userInput = Console.ReadLine().ToLower()) != "exit") { if (openWith.ContainsKey(userInput)) Console.WriteLine(openWith[userInput]); else Console.WriteLine("Doesn't exist"); Console.WriteLine("Type another bug check, or \"exit\" to quit:\n"); } } 

use ContainsKey to check if the user enters a value that is in your dictionary. If so, use the openWith[userInput] index to get the value. If no error is displayed.

In no case do you need a special class to implement your own dictionary. A dictionary is just a general collection of KeyValuePairs. It makes sense for you to use Dictionary<string, string> here - there is no need to complicate the work.

You said, β€œI have no idea,” but you were very close.

+3
source share

All Articles