Fixing future problematic code?

I'm a very new C# programmer, less than ~24 actual hours of raw typing/programming. I'd say about a week however of reading, watching, and trying to understand things.

Here's the source code to 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_EQUALn" +
                    "This indicates that a kernel-mode driver attempted to access pageable memory atn" + 
                    "a process IRQL that was too high.n";
            else if (userInput == "0xa")
                message = "IRQL_NOT_LESS_OR_EQUALn" +
                    "This indicates that Microsoft Windows or a kernel-mode driver accessedn" +
                    "paged memory at DISPATCH_LEVEL or above.n";
            else if (userInput == "0x1e")
                message = "KMODE_EXCEPTION_NOT_HANDLEDn" +
                    "This indicates that a kernel-mode program generated an exception which then" +
                    "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 it to. The user types the bug check code they're getting, it shoots back the very basic MSDN-MSFT definition, and asks for you to either type another bug check code, or exit to quit the program.

However, according to videos/reading materials, an important thing to learn is how to spot code that will be a problem in the future. Well, from my perspective at least, the method in which I'm using to check the value of the user's input regarding the bug check is potentially not good, because it'll eventually be way too large and messy given the number of bug checks there are.

So I did some research and found out about dictionaries, neat. 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 accessedn" + 
            "paged memory at DISPATCH_LEVEL or above.n" },
    { "0xd1", "This indicates that a kernel-mode driver attempted to access pageable memory atn" + 
            "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 to myself the entire time has been "How the heck do I implement this into my current code?", and I still don't really know which shames me to say. I've tried looking up and researching creating/managing new Classes, and although it was a little confusing, I got the basic idea of it and wrote a quick program that doesn't ask for user input, but instead just 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's 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 sort of understand the basics of dictionaries and classes, but I still have no idea how to implement the/a better method into my current "bug check" program.

Any advice, tips? I tried to make my post to show that I was trying as much as possible to figure it out on my own because I know StackOverflow frowns upon people just trying to get others to code for them, I'm just not sure where else to look/read to figure it out, or how to at least step it in the right direction.


Here's 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 accessedn" + 
                    "paged memory at DISPATCH_LEVEL or above.n" },
            { "0xd1", "This indicates that a kernel-mode driver attempted to access pageable memory atn" + 
                    "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 inputted a value that is in your Dictionary. If it is, use the index openWith[userInput] to get the value. If not display the error.

By no means do you need a custom class to implement your own dictionary. A Dictionary is just a generic collection of KeyValuePairs. It makes sense for you to use a Dictionary<string, string> here - no need to make it more complicated.

You said "I have no idea", but you were really quite close.

链接地址: http://www.djcxy.com/p/27098.html

上一篇: 在json中格式化Flask应用程序日志

下一篇: 修复未来有问题的代码?