简体   繁体   English

无法将类型类的对象强制转换为“ System.String”类型

[英]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将选定的项目KlasaBetonaaaaKlasaBetonaaaaFck属性(为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作为一个单独的实例KlaseBetonastring 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 KlasaBetonaKlasaBetona并首先使用该属性:

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.

相关问题 无法将类型“ System.String”强制转换为类型“ System.Object” - Unable to cast the type 'System.String' to type 'System.Object' 无法将“DapperRow”类型的对象转换为“System.String”类型 - Unable to cast object of type 'DapperRow' to type 'System.String' 无法将“System.string”类型的对象转换为“....Event”类型 - Unable to cast object of type 'System.string' to type '....Event' 无法将“AttachmentDetailsDto”类型的对象转换为“System.String”类型 - Unable to cast object of type 'AttachmentDetailsDto' to type 'System.String' 无法将类型为“ System.String”的对象转换为类型为“ FileItem”的对象 - Unable to cast object of type 'System.String' to type 'FileItem' 无法将“System.String”类型的对象转换为“System.Collections.Generic.List`1[System.String]”类型 - Unable to cast object of type 'System.String' to type 'System.Collections.Generic.List`1[System.String]' 如何将字符串类型转换为 class 类型? 这是错误消息:-无法将类型为“System.String”的 object 转换为类型“query” - How do i cast a string type to a class type ? here is the ERROR MESSAGE:- Unable to cast object of type 'System.String' to type 'query' C# 中的 MS Word 自动化 - 无法将类型为“System.String[*]”的 object 转换为类型“System.String[]” - MS Word Automation in C# - Unable to cast object of type 'System.String[*]' to type 'System.String[]' 无法将对象类型“ System.String [*]”转换为类型“ System.String []” - Unable To Cast object type 'System.String[*]' to type 'System.String[]' 如何解决“无法在CallBimlScript中强制转换&#39;System.String&#39;类型的对象” - How to troubleshoot “Unable to cast object of type 'System.String'” in CallBimlScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM