Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I know this has been all over the web but for some reason mines not working

this is my code



C#
  int move = 1;
private void Play_Click(object sender, EventArgs e)
     {
int move = Convert.ToInt32(textBox1.Text);
timer2.Start();
}


And this is the timer tick code

C#
private void timer2_Tick_1(object sender, EventArgs e)
    {
        BeatDetector.Location = new Point(BeatDetector.Location.X + move, BeatDetector.Location.Y);
    }


whats meant to happen is, I the what ever number into the picturebox and the speed of the picturebox(BeatDetector) will increase/decrease speed of it when its moving on the X, but mine seems not to do anything, im a bit confused
Posted
Updated 7-Jun-12 0:13am
v2

You can use Convert.ToInt32(String)[^] or Int32.Parse(String) [^] or Int32.TryParse(String)[^] methods, but the basic difference is:

Note:

Using the ToInt32 method is equivalent to passing value to the Int32.Parse(String) method. Value is interpreted by using the formatting conventions of the current thread culture.

If you prefer not to handle an exception if the conversion fails, you can call the Int32.TryParse method instead. It returns a Boolean value that indicates whether the conversion succeeded or failed.


[EDIT]
VJ is GREAT, has a hawk eye! I'm a blind man!
C#
int move = 1;
private void Play_Click(object sender, EventArgs e)
     {
int move = Convert.ToInt32(textBox1.Text);

Of course, you have declared int move twice. First time outside the procedure and second, inside the procedure. That's the reason of your troubles.
Read more about: Variable and Method Scope[^]
[/EDIT]
 
Share this answer
 
v2
Comments
BillW33 7-Jun-12 12:39pm    
Good explanation, especially for a blind man. ;) It is much better to use TryParse then to simply use the straight ToInt32. I’ll give you a +4. :)
Maciej Los 7-Jun-12 12:53pm    
4 is OK ;)
Thank you, CIDev.
BillW33 7-Jun-12 14:54pm    
Well, you updated your answer, I reckon I will update my vote to a well deserved +5 :)
VJ Reddy 7-Jun-12 12:54pm    
Good answer and thank you very much for mentioning my name. 5!
Maciej Los 7-Jun-12 14:01pm    
You're welcome ;) That's a prove of my thankful for you...
The reason may be that in the code
C#
int move = 1;
private void Play_Click(object sender, EventArgs e)
{
    int move = Convert.ToInt32(textBox1.Text);
    timer2.Start();
}

the outer int move statement declares a class level scoped move variable (field), and the int move statement in side the Play_Click method declares a local variable inside the method. Whatever changes made to the move variable inside this method are lost once the execution comes out of this method.

So the value 1 of the class level move variable is only used all the time in the method timer2_Tick_1, hence no change is noticed.

To retain the value assigned to move replace
int move = inside Play_Click method with
move =

Please see the following code to visualize the above in points
C#
void Main()
{
    VarScopeTest varScope = new VarScopeTest();

    varScope.method1(5);
    varScope.method2();
    varScope.method1(10);
    varScope.method2();
}

public class VarScopeTest {
    int move =1;
    int correctMove =1;
    public void method1(int value){
        int move =value;
        correctMove = value;
        Console.WriteLine ("move in side method 1={0}",move);
        Console.WriteLine ("correctMove in side method 1={0}\n",correctMove);
    }
    public void method2(){
        Console.WriteLine ("move in side method 2={0}",move);
        Console.WriteLine ("correctMove in side method 2={0}\n",correctMove);
    }
}

//Output
//move in side method 1=5
//correctMove in side method 1=5
//
//move in side method 2=1
//correctMove in side method 2=5
//
//move in side method 1=10
//correctMove in side method 1=10
//
//move in side method 2=1
//correctMove in side method 2=10
 
Share this answer
 
v4
Comments
Maciej Los 7-Jun-12 12:27pm    
...the scopes of variables! Am i blind or something?
Hawkeye!
+5+5+5!
BillW33 7-Jun-12 12:36pm    
Yeah, we all have that blindness from time to time ;)
VJ Reddy 7-Jun-12 13:00pm    
Thank you very much, losmac :D
BillW33 7-Jun-12 12:36pm    
And a +5 to the sharp eyed VJ. :)
VJ Reddy 7-Jun-12 13:00pm    
Thank you very much, CIDev :D
hi
we can user int.Parse method to convert string to int

String to int[^]
 
Share this answer
 
Comments
[no name] 7-Jun-12 6:14am    
so why was my method bad?
Maciej Los 7-Jun-12 6:25am    
Please, read my answer.
C#
int move = 1;
private void Play_Click(object sender, EventArgs e)
     {
int move = (Int)textBox1.Text;
timer2.Start();
}
 
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