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:
const int moveLeft = 1;
const int moveRight = 2;
const int moveUp = 4;
const int moveDown = 8;
And pass them instead:
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!