[英]Unable to cast object of type class to type 'System.String'
Class 1 1类
public class KlasaBetonaaaa
{
public string Naziv { get; set; }
public double _Fck { get; set; }
}
Class 2 2级
public static class Database
{
public static KlasaBetonaaaa[] GetAllKlasaBetona()
{
return new KlasaBetonaaaa[]
{
new KlasaBetonaaaa
{
Naziv = "C12/15",
_Fck = 12.0
};
}
}
}
Class 3 3级
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
{
var klaseBetona = Database.GetAllKlasaBetona();
klasaBetonaComboBox.Items.AddRange(klaseBetona);
}
private void klasaBetona_SelectedIndexChanged(object sender, EventArgs e)
{
var selectedBeton = klasaBetonaComboBox.SelectedItem;
CalculateSomething((string)selectedBeton);
}
private void CalculateSomething(string selectedBeton)
{
fck.Text = selectedBeton;
}
}
This string selectedBeton is what i tried before with only 1 name converting to string from collection. 这个字符串selectedBeton是我之前尝试过的,只有1个名称从collection转换为string。 Now i created new classes (Manual database)as and array and i dont know how to link it so when i pick "C12/15" to get _Fck value 15.0 in a label.
现在,我创建了新的类(手动数据库)作为数组,并且我不知道如何链接它,所以当我选择“ C12 / 15”以在标签中获得_Fck值15.0时。
klasaBetonaComboBox.SelectedItem
is of type KlasaBetonaaaa
, since you add an array of that type ( klasaBetonaComboBox.Items.AddRange(klaseBetona);
). klasaBetonaComboBox.SelectedItem
的类型为KlasaBetonaaaa
,因为您添加了该类型的数组( klasaBetonaComboBox.Items.AddRange(klaseBetona);
)。 That means that casting SelectedItem
to string
will fail. 这意味着将
SelectedItem
强制转换为string
将失败。
You should cast the selected item to KlasaBetonaaaa
and cast the Fck
property, which is a double
, using ToString
(or if you want to take the Naziv
string property, you don't need ToString()
, since it is one already): 您应该使用
ToString
将选定的项目KlasaBetonaaaa
为KlasaBetonaaaa
并Fck
属性(为double
(或者,如果您想获取Naziv
字符串属性,则不需要ToString()
,因为它已经是一个):
KlasaBetonaaaaselectedBeton = klasaBetonaComboBox.SelectedItem;
CalculateSomething(selectedBeton.Fck.ToString());
It seems that you have used an IEnumerable<KlasaBetona>
as datasource for the combobox. 看来您已经使用
IEnumerable<KlasaBetona>
作为组合框的数据源。 Now you're trying to cast the SelectedItem
which is a single instance of KlaseBetona
to string
. 现在,您正在尝试将
SelectedItem
作为一个单独的实例KlaseBetona
为string
。 That doesn't work since you haven't overloaded the explicit
-conversion operator like this: 这不起作用,因为您没有像这样重载
explicit
-conversion运算符 :
public static explicit operator string(KlasaBetona kl)
{
return kl._Fck.ToString();
}
But instead of that i suggest to cast the SelectedItem
to KlasaBetona
and use that property in the first place: 但是相反,我建议将
SelectedItem
KlasaBetona
为KlasaBetona
并首先使用该属性:
KlasaBetona kl = (KlasaBetona) selectedBeton;
CalculateSomething(kl._Fck.ToString());
You need to cast to the actual type and then show the string of _Fck
, you could always override the ToString
method of your class and show that instead. 您需要转换为实际的类型,然后显示
_Fck
的字符串,您始终可以覆盖类的ToString
方法,并进行显示。 Alternatively, if you do calculations on the object then you can pass that into the method instead. 或者,如果对对象进行计算,则可以将其传递给方法。
var selectedBeton = klasaBetonaComboBox.SelectedItem as KlasaBetonaaaa;
if(selectedBeton != null)
CalculateSomething(selectedBeton._Fck.ToString());
In my case I got similar message Unable to cast object of type class to type Enum
when I use JsonConverter to Serialize class. 在我的情况下,当我使用JsonConverter序列化类时,我收到类似消息:
Unable to cast object of type class to type Enum
类型。
Finally I found that I forgot to remove attribute [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
on my string property, after remove the attribute, the problem is solved. 最后,我发现我忘记删除字符串属性上的属性
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
,删除属性后,问题得以解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.