指定的强制转换在我的项目C#中无效 [英] Specified cast is not valid in my project C#
问题描述
public char UserID
{
get {return(char)cboUser.SelectedValue; }
}
/// --------------------- -------------------------------------------------- -
我的价值用户ID是'00436'来自组合框
如何解决这个前任。
我尝试过:
公共字母用户ID
{
get { return(char)cboUser.SelectedValue; }
}
/// --------------------- -------------------------------------------------- -
我的值用户ID是'00436'来自combobox
public char UserID
{
get { return (char)cboUser.SelectedValue; }
}
///-------------------------------------------------------------------------
my value UserID is '00436' from combobox
how can i resolve this ex.
What I have tried:
public char UserID
{
get { return (char)cboUser.SelectedValue; }
}
///-------------------------------------------------------------------------
my value UserID is '00436' from combobox
推荐答案
不要试图将它强制转换为char
!
Achar
是单个字符,是字符串的一部分
。
例如,Hello包含五个char
值:'H','e',2'l's和'o'。
您无法将字符串
直接转换为char
任何超过你可以直接将一个完整的停车场转换成一辆车!
您的UserID值不太可能是char
输入 - 它更可能是字符串
或int
- 所以检查什么是ComboBox并相应地更改整个属性。这可能意味着您使用加载ComboBox的代码进行检查并查看它从何处获取数据 - 如果可能的话,您希望使用本机类型。
Don't try to cast it to achar
!
Achar
is a single character, a part of astring
.
For example, "Hello" contains fivechar
values: 'H', 'e', 2 'l's, and an 'o'.
You can't convert astring
directly to achar
any more than you can directly convert a full car park into a single car!
Your UserID value is very unlikely to be achar
type at all - it's more likely to be astring
or anint
- so check what is in the ComboBox and change the whole property accordingly. This will probably mean you checking with the code that loads the ComboBox and seeing where it gets it's data from - you want to use the native type if at all possible.
这篇关于指定的强制转换在我的项目C#中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!