-1

I'm coding a simple fruit machine and this is one of my methods, I was wondering if it was possible to make this code more efficient: (Before using case i had it an if / else if statement)

_intNudgeCount gets a number between 0 - 9 hence the case.

public void DrawNudgeCount()
    {
        switch (_intNudgeCount)
        {
            case 9:
                pictureBoxNudgeCount.Image = Properties.Resources._9;
                break;
            case 8:
                pictureBoxNudgeCount.Image = Properties.Resources._8;
                break;
            case 7:
                pictureBoxNudgeCount.Image = Properties.Resources._7;
                break;
            case 6:
                pictureBoxNudgeCount.Image = Properties.Resources._6;
                break;
            case 5:
                pictureBoxNudgeCount.Image = Properties.Resources._5;
                break;
            case 4:
                pictureBoxNudgeCount.Image = Properties.Resources._4;
                break;
            case 3:
                pictureBoxNudgeCount.Image = Properties.Resources._3;
                break;
            case 2:
                pictureBoxNudgeCount.Image = Properties.Resources._2;
                break;
            case 1:
                pictureBoxNudgeCount.Image = Properties.Resources._1;
                break;
            case 0:
                pictureBoxNudgeCount.Image = Properties.Resources._0;
                break;
        }
    }

Thanks in advance!

SOLVED:

It's okay, I've got it down to 3 lines of code:

//declare resource images at top of the class.

private System.Drawing.Image[]  _arrayNudgeCount;

//Populated the array when loading the class.

_arrayNudgeCount = new System.Drawing.Image[] { Properties.Resources._0, Properties.Resources._1};

//Redraw images

public void DrawNudgeCount()
{
pictureBoxNudgeCount.Image = _arrayNudgeCount[_intNudgeCount];
}

2 Answers2

0

place your Image in a folder beside of your appilication and call pictureBoxNudgeCount.Load(ConstantSectionPath+_intNudgeCount+".jpg"); if jpg is your file extensions.

mojtaba
  • 339
  • 1
  • 4
  • 17
0

I would have reduced it to just one line of code I were you!

pictureBoxNudgeCount.Image = (Image)Properties.Resources.ResourceManager.GetObject("_" + _intNudgeCount);
Prahlad Yeri
  • 3,567
  • 4
  • 25
  • 55