gpt4 book ai didi

c# - 将组合框字符串值转换为 int

转载 作者:行者123 更新时间:2023-11-30 14:19:23 25 4
gpt4 key购买 nike

我有一个关于转换类型的问题。我想将当前选择的组合框值字符串更改为一个 int,但出现错误

我的代码:

int.Parse(age.SelectedItem.ToString());

我能为这个问题做什么?

最佳答案

好的,现在我们知道了错误,您可以在尝试解析它之前检查空值:

    if (comboBox1.SelectedItem != null)
{
int x = int.Parse(comboBox1.SelectedItem.ToString());
}
else { //Value is null }

您可以通过将控件的文本属性设置为您想要的任何默认值来避免传递空值。

如果您在确定选择一个后仍然没有得到值,那么您确实需要发布您的代码。

关于c# - 将组合框字符串值转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2831082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com