English 中文(简体)
如何优化这一守则?
原标题:How can i optimize this code?

我有以下法典:

    if (userValueSom01 == realResult01)
    {
        //answer = correct
        //count +1 for overall good answers
        WpfApplication1.Properties.Settings.Default.totallGood++;
        //count for good +1
        answerThisWindowGood++;
        //make visible the green logo
        Som01G.Visibility = Visibility.Visible;
    }
    else
    {
        //answer = wrong
        //count +1 for overall wrong answers
        WpfApplication1.Properties.Settings.Default.totallWrong++;
        //count for wrong +1
        answerThisWindowWrong++;
        //make visible the red logo
        Som01W.Visibility = Visibility.Visible;
        labelSom01Check.Content = Convert.ToString(realResult01);
    }

Now the point is, this happens XX times, where XX is a number corresponding with the numbers you see within the code. So in the above example the XX is 01. *note, its the 01 s in the input, and the 01 in the results too

In not very deep into c# (yet), and at first i thought that when XX is 20, i will need to copy this above part 20 times, and change the numbers. Now this seems cumbersome, and i guess there should be some smarter way ti deal with this, point is, i cant think of how (as written above, im not very deep into c# yet).

谁能把我推向正确的方向?

事先感谢你。

---EDIT 1--- thank you Miika L. slightly different from your solution:

public bool checkValue(double value, int result, Image controlG, Image controlW, Label label)
        {
            if (value == result)
            {
                //... Do stuff
                controlG.Visibility = Visibility.Visible;
                return true;
            }
            else
            {
                //... Do other stuff
                controlW.Visibility = Visibility.Visible;
                label.Content = result.ToString();
                return false;
            }
        }

and now i can indeed just call: bool test = checkValue(userValueSom01, realResult01, Som01G, Som01W, labelSom01Check);

works :) thanx!

最佳回答

如何将其写成一个职能?

public bool checkValue(
    int value,
    int result,
    Control controlG,
    Control controlW,
    Label label)
{
    if (value == result)
    {
        ... Do stuff
        controlG.Visibility = Visibility.Visible;
    }
    else
    {
        ... Do other stuff
        controlW.Visibility = Visibility.Visible;
        label.Content = result.ToString();
    }
}
问题回答

实际Result01不是用用户ValueSom01等名称来界定数以百计的变量,而是更好地使用Array(如果有的话)。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...