如何将字符串转换为整数 [英] How to convert a string to an integer

查看:101
本文介绍了如何将字符串转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字符串转换为int



I am trying to convert a string to an int

int editScore =0;
            
           
         //   editScore = Convert.ToInt32(Label1.Text) - Convert.ToInt32(report[2]);
            editScore = int.Parse(Label1.Text);





但我收到此错误





But I am getting this error

When converting a string to a Datetime, parse the string to take the date before putting each variable into the datetime object 





谢谢



我的尝试:





thanks

What I have tried:

int editScore =0;
            
           
         //   editScore = Convert.ToInt32(Label1.Text) - Convert.ToInt32(report[2]);
            editScore = int.Parse(Label1.Text);

推荐答案

editScore = int.Parse(Label1.Text);



这行代码不可能产生此错误消息。 />
使用调试器,它会显示错误的位置,通过检查变量,您将能够看到崩溃程序的值。

如果值是不是在变量中,在中间变量中加入它你会看到。



当你不明白你的代码在做什么或为什么它做它做的事情,答案是调试器

使用调试器查看代码正在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么以及您的任务是与它应该做的比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个错误。


This line of code cannot possibly produce this error message.
Use the debugger, it will show you where is the error, and by inspecting the variables, you will be able to see what value is crashing your program.
If the value is not in a variable, stire it in an intermediate variable and you will see.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


我假设代码试图用样式转换字符串(1,234)(例如逗号)我会建议以下



I'm assuming the code is trying to convert a string ("1,234) with styles (example comma) into integer. I would suggest the following

int output = 0;
//null = use current culture
            if (int.TryParse("1,233", System.Globalization.NumberStyles.AllowThousands |
                    System.Globalization.NumberStyles.AllowLeadingSign, null, out output))
            {
                Console.WriteLine(output);
            }





更多阅读:

编码挑战经验教训 [ ^ ]

NumberStyles Enumeration(System.Globalization) [ ^ ]


这篇关于如何将字符串转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆