1

I received an error:

'MoveBall.Game' does not contain a definition for 'ballSpeedAxis1' and no extension method 'ballSpeedAxis1' accepting a first argument of type 'MoveBall.Game' could be found (are you missing a using directive or an assembly reference?)

In the beginning, there was no error when I tried to insert the value one by one.

int ballSpeedAxis1 = 1;
int ballSpeedAxis2 = 1;
...
int ballSpeedAxis10 = 1;

However, after I changed to a for loop as shown below.

var ballSpeedXAxis = new int[10];
for (int i = 0; i < ballSpeedXAxis.Length; i++)
{
    ballSpeedXAxis[i] = 1;
}

The error occurs on the following lines:

private void OnUpdate(object sender, object e)
{
    Canvas.SetLeft(this.ball1, this.ballSpeedXAxis + Canvas.GetLeft(this.ball1));
}

May I know how can I solved it?

14
  • 1
    I don't see where you're using Canvas.SetLeft(this.ball1, this.ballSpeedXAxis + Canvas.GetLeft(this.ball1)); so it's hard to say why there is an error. Please show all of the relevant code. Commented Jul 24, 2013 at 4:18
  • @Yuck Hi, do take a look at it again. Commented Jul 24, 2013 at 4:22
  • Welcome to stackoverflow.com..Thankyou for Asking the Q may i know what is the error shown? Commented Jul 24, 2013 at 4:25
  • 1
    You must still be referencing ballSpeedAxis1 somewhere but have not changed that to use the new array. Commented Jul 24, 2013 at 4:33
  • 2
    Your error message includes 'ballSpeedAxis1. Are you using ballSpeedAxis1` instead of ballSpeedXAxis accidentally? Commented Jul 24, 2013 at 4:35

2 Answers 2

2

Because your variable scope is wrong. ballSpeedXAxis should be defined inside the class instead of inside the method that initializes it. Also, you cannot use var if you define a class-scoped variable. And it is weird that you don't use index inside OnUpdate method.

class MyClass {

    int[] ballSpeedXAxis = new int[10];

    MyClass() { // constructor
       for (int i = 0; i < ballSpeedXAxis.Length; i++)
       {
            ballSpeedXAxis[i] = 1;
       }
    }

    private void OnUpdate(object sender, object e)
    {
        Canvas.SetLeft(this.ball1, this.ballSpeedXAxis[<some index here>] + Canvas.GetLeft(this.ball1));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You changed ballSpeedXAxis to an array, yet in your OnUpdate you are trying to access it as if it were a normal integer:

private void OnUpdate(object sender, object e)
{
     Canvas.SetLeft(this.ball1, this.ballSpeedXAxis + Canvas.GetLeft(this.ball1));
}

You must specify the index of the int in the array you whish to use

this.ballSpeedXAxis[0] for example.

Now you say the error is:

'MoveBall.Game' does not contain a definition for 'ballSpeedAxis1

If you're using VisualStudio, MonoDevelop or Xamarin Studio clicking the error item in the Errors Window should take you right to the point in the code where you are still refering to ballSpeedAxis1.

You could even try to hit CTRL+F and enter 'ballSpeedAxis1'
in the search box and search through the entire solution.

If you cannot find the location using either method, then something might be out of sync and you should use the 'CLEAN SOLUTION' and 'REBUILD SOLUTION' functions of your IDE.

3 Comments

@TimonthyP Hi, I have tried this.ballSpeedXAxis[1]. However, I still received the same error.
Do they both reside in different class ?
@jparthj Hi, both are in the same class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.