指定的强制转换在我的项目C#中无效 [英] Specified cast is not valid in my project C#

查看:79
本文介绍了指定的强制转换在我的项目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

A char 是单个字符,是字符串的一部分

例如,Hello包含五个 char 值:'H','e',2'l's和'o'。

您无法将字符串直接转换为 char 任何超过你可以直接将一个完整的停车场转换成一辆车!



您的UserID值不太可能是 char 输入 - 它更可能是字符串 int - 所以检查什么是ComboBox并相应地更改整个属性。这可能意味着您使用加载ComboBox的代码进行检查并查看它从何处获取数据 - 如果可能的话,您希望使用本机类型。
Don't try to cast it to a char!
A char is a single character, a part of a string.
For example, "Hello" contains five char values: 'H', 'e', 2 'l's, and an 'o'.
You can't convert a string directly to a char any more than you can directly convert a full car park into a single car!

Your UserID value is very unlikely to be a char type at all - it's more likely to be a string or an int - 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屋!

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