Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The Goal here is to move a pumpkin image using four buttons(Left, Right, Up, and Down). Using switch case method to work with directions. I have a NumericUpandDown to decide how many jumps the user wants(1-10) and four buttons that lets the user move the image. I need parameters of jumps as an integer and directions as a string.

What I have tried:

C#
public void MovePumpkin(int Jumps, string Directions)
        {

            switch (Directions)
            {
                case 1:
                    picGreatPumpkin.Left = picGreatPumpkin.Left - Jumps;
                    return;
                case 2:
                    picGreatPumpkin.Left = picGreatPumpkin.Left + Jumps;
                    return;
                case 3:
                    picGreatPumpkin.Top = picGreatPumpkin.Top - Jumps;
                    return;
                case 4:
                    picGreatPumpkin.Top = picGreatPumpkin.Top + Jumps;
                    return;


            }
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {

            int Jumps = Convert.ToInt32(Math.Round(numDistance.Value, 0));
            MovePumpkin(Directions);
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
           
        }
Posted
Updated 17-Feb-22 3:05am
v2
Comments
charles henington 16-Feb-22 19:19pm    
Directions is a string but in your case statement you treat it as an int
switch (Direction)
{
case "Forward":
//do something here
break;
case "Back":
//do something here
break;
}

If you're expecting an int then you will need to convert the string to int or change Directions from string Directions to int Directions. There's many possibilities to what Directions are a reference to. What value is passed to Directions?

1 solution

You need to think about what you are doing before you dive into code.
Firstly, if your Directions contains a string, then you can't use a switch that assumes it is an integer - one or the other will work, but together it won't. So either Directions needs to be an integer, or the case statements must be a string.

Second, when you call your MovePumpkin method, you only supply one of the required two parameters:
public void MovePumpkin(int Jumps, string Directions)
...
MovePumpkin(Directions);
You don't pass the Jumps count, so teh compiler won;t let you do that.

Thirdly, your don't set the Directions to anything when you do pass it - so it doesn;t know if you want up, down, left, or right!

Instead of faffing with strings, create four const values:
C#
const int moveLeft = 1;
const int moveRight = 2;
const int moveUp = 4;
const int moveDown = 8;
And pass them instead:
C#
public void MovePumpkin(int jumps, int directions)
        {
            switch (directions)
            {
                case moveLeft:
                    picGreatPumpkin.Left = picGreatPumpkin.Left - jumps;
                    return;
...
            }
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {

            int jumps = (int) numDistance.Value;
            MovePumpkin(jumps, moveLeft);
        }
...
Your code becomes more readable, and more reliable.
If you have covered enum on your course yet, then use one of those instead of a integer and replace the const values for even better code!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900