运行时错误使用int :: Parse On ListBox项 [英] Runtime Error Using int::Parse On ListBox Item

查看:75
本文介绍了运行时错误使用int :: Parse On ListBox项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在64位Win10机器上使用VS2012 C ++ / CLI。

I am using VS2012 C++/CLI on a 64bit Win10 machine.

我有一个填充了一些字符串的列表框。 我正在尝试使用这些字符串并使用Parse来获得对应的数字。

I have a listbox filled with some Strings.  I am trying to take those Strings and use Parse to get a number that corresponds.

我在其他地方使用了这个名称的按钮,例如:

I have used this in other places with the name of a button, e.g.:

            LS-> StyleSave(int :: Parse(ButtonSaveStyle-> Name));

            LS->StyleSave(int::Parse(ButtonSaveStyle->Name));

这里,Parse函数的结果是一个相关的数字到按钮的名称。 此代码工作正常。

Here, the result of the Parse function is a number related to the Name of the button.  This code works fine.

现在,我正在尝试使用存储在列表框中的字符串执行相同的操作。 

Now, I am trying to do the same thing with Strings stored in a listbox. 

因此,我有:

                    tString = listBoxStlyeOrder-> Items [0] - > ToString();

                     tParse = int :: Parse(tString);

                    tString = listBoxStlyeOrder->Items[0]->ToString();
                    tParse = int::Parse(tString);

此代码编译正常,但随后在Parse语句上崩溃(不是语句加载tString)。

This code compiles fine but then crashes on the Parse statement (not the statement loading tString).

tString是^ String,而tParse是int。

tString is a ^String while tParse is an int.

使用断点,我可以看到tString实际上是用预期的字符串加载的。

Using a breakpoint, I can see that tString is actually loaded with the intended string.

我注意到的一件事是Intellisense突出了Blue中的前五个字母。

One thing I notice is that Intellisense highlights the first five letters in Blue.

任何有关我弄清楚为什么会得到帮助的帮助这次失败表示赞赏。

Any help on figuring out why I am getting this crash appreciated.

推荐答案


我有一个填充了一些字符串的列表框。 我正在尝试使用这些字符串并使用Parse来获得对应的数字。

I have a listbox filled with some Strings.  I am trying to take those Strings and use Parse to get a number that corresponds.

                     tParse = int :: Parse(tString);

                    tParse = int::Parse(tString);

此代码编译正常,但随后在Parse语句上崩溃(不是语句加载tString)。

This code compiles fine but then crashes on the Parse statement (not the statement loading tString).

tString是^ String,而tParse是int。

tString is a ^String while tParse is an int.

运行时会发生什么,*确切*? "碰撞"是一个通用术语,可以


意味着什么。我们需要更具体的内容。



它是否会抛出异常?如果是这样,那是什么?这是一种无效的格式吗?



通常使用TryParse而不是Parse会更好,因为它允许你在
检查字符串的有效性而不用触发异常。



Int :: Parse要求字符串只包含有效字符为
的整数。



- Wayne

What happens at run time, *exactly*? "crash" is a generic term that could
mean just about anything. We need to have something more specific.

Is it throwing an exception? If so, what kind? Is it an invalid format?

It's usually better to use TryParse instead of Parse as it allows you to
check the string for validity without triggering an exception.

Int::Parse expects the string to contain only characters that are valid
for an Integer.

- Wayne


这篇关于运行时错误使用int :: Parse On ListBox项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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