Click here to Skip to main content
15,909,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Project and getting this error. Unable to cast object of type 'System.String' to type 'Item'.

The error is on this line

var item = (Item)comboBox1.SelectedItem;

I have tried everything but the right thing.

full code

C#
<pre>public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }

        

        private void Form1_Load(object sender, EventArgs e)
        {
            var itemList = new List<Item>()
            {
                new Item() { Text = "L", Value = 1.75M },
                new Item() { Text = "US fl oz", Value = 1.698M }
            };

            comboBox1.DataSource = itemList;
            comboBox1.DisplayMember = "Text";
            comboBox1.ValueMember = "Value";

            comboBox2.DataSource = itemList;
            comboBox2.DisplayMember = "Text";
            comboBox2.ValueMember = "Value";
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                var item = (Item)comboBox1.SelectedItem;
                MessageBox.Show($"{item.Text} - {item.Value}");


                textBox4.Text = ((Convert.ToDouble(textBox2.Text) / Convert.ToDouble(textBox3.Text)
                - Convert.ToDouble(textBox10.Text)) * Convert.ToDouble(textBox1.Text) / Convert.ToDouble(comboBox1.SelectedItem)).ToString("0.00");


            }
        }


What I have tried:

lots of looking on the internet and other program code
Posted
Updated 13-Dec-17 22:17pm
v2
Comments
Karthik_Mahalingam 13-Dec-17 22:58pm    
show the Item class code

1 solution

The Property SelectedItem does not return an Item, it returns an Object. MSDN reference for the SelectedItem property - MSDN - ComboBox.SelectedItem Property[^]
You could change your code as follows;
C#
// current text value
string strItemText = comboBox1.Text;
// convert the selected value to a Decimal value - always use TryParse & handle errors
decimal decItemValue;
if(!Decimal.TryParse(comboBox1.SelectedValue.ToString(), out decItemValue))
{ // invalid value - handle error
}

Use your debugger, between reference information - MSDN is great but there are others - and your debugger you will learn more about coding than you think

Your code;
C#
var item = (Item)ComboBox.SelectedItem;
is attempting to unbox an object to a value type, this only works for explicit conversions, if the conversion is not explicit then the conversion will fail - refer; Boxing and Unboxing (C# Programming Guide) | Microsoft Docs[^]

C# is a type-strong language & you should use the correct type for variables - IMO. In this case I would see the variable type "var" and assume you were being lazy. There is a use for the var type but this is not it - refer; var (C# Reference) | Microsoft Docs[^]

Kind Regards
 
Share this answer
 
v2
Comments
Member 12349103 13-Dec-17 19:15pm    
Bear with me I'm new add the lines to the button click.
and got error "Object reference not set to an instance of an object.
on line
if (!Decimal.TryParse(comboBox1.SelectedValue.ToString(), out decItemValue))
an0ther1 13-Dec-17 19:25pm    
Use your debugger. 'Object reference not set...' is telling you that one of the values you are expecting is nothing.
Insert a breakpoint in you code before this line, for instance on the line 'string strItemText'
When your code breaks into the debugger test each value in the Immediate Window by entering your command (preceded by a question mark) & pressing enter - eg;
? comboBox1.SelectedValue

Kind Regards
Member 12349103 14-Dec-17 12:41pm    
I accept this answer, as it guided in the direction to fix the problem.
an0ther1 14-Dec-17 15:33pm    
Thank-you. Glad to help, good luck

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900