且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#将字符串转换为整数

更新时间:2022-11-09 10:46:25

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

注意:


使用ToInt32方法等效于将值传递给Int32.Parse(String)方法.通过使用当前线程区域性的格式约定来解释值.

如果不想在转换失败的情况下不处理异常,则可以改为调用Int32.TryParse方法.它返回一个布尔值,该值指示转换是成功还是失败.


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.




VJ很棒,有鹰眼!我是个盲人!




VJ is GREAT, has a hawk eye! I''m a blind man!

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


当然,您已经两次声明了int move.第一次在程序外,第二次在程序内.那就是你麻烦的原因.
阅读有关以下内容的更多信息:变量和方法范围 [


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]


原因可能是代码中的原因
The reason may be that in the code
int move = 1;
private void Play_Click(object sender, EventArgs e)
{
    int move = Convert.ToInt32(textBox1.Text);
    timer2.Start();
}


外部int move语句声明了范围为move变量 (field)的类级别,而int move 方法旁边的int move 语句在方法内部声明了local variable. 一旦执行此方法,对该方法内move变量所做的任何更改都将丢失.

因此,class level move变量的value 1仅在方法timer2_Tick_1中始终使用,因此没有任何变化.

要保留分配给移动的值,请替换
Play_Click 方法内的int move =
move =

请查看以下代码以直观显示以上内容


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

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


hi
我们可以使用int.Parse方法将字符串转换为int

要转换为整数的字符串 [
hi
we can user int.Parse method to convert string to int

String to int[^]