1

Class 1

public class KlasaBetonaaaa
     {
         public string Naziv { get; set; }
         public double _Fck { get; set; }
     }

Class 2

 public static class Database
     {
         public static KlasaBetonaaaa[] GetAllKlasaBetona()
         {
             return new KlasaBetonaaaa[]
             {
                 new KlasaBetonaaaa
                 {
                     Naziv = "C12/15",
                     _Fck = 12.0
                 };
             }
         }
     }

Class 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. 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.

3
  • 2
    On which line exactly? Commented Mar 16, 2015 at 8:01
  • 1
    I think the problem is that selectedBeton at the line var selectedBeton = klasaBetonaComboBox.SelectedItem; is not a string, but a KlasaBetonaaaa. However, please provide more information about the error you are getting. Commented Mar 16, 2015 at 8:04
  • An unhandled exception of type 'System.InvalidCastException' occurred in WindowsFormsApplication3.exe Additional information: Unable to cast object of type 'WindowsFormsApplication3.KlasaBetonaaaa' to type 'System.String'. This is what i wanna do here: When i pick Naziv in combobox that i cant get and link _Fck with the rest of my code. Commented Mar 16, 2015 at 8:08

4 Answers 4

2

klasaBetonaComboBox.SelectedItem is of type KlasaBetonaaaa, since you add an array of that type (klasaBetonaComboBox.Items.AddRange(klaseBetona);). That means that casting SelectedItem to string will fail.

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):

KlasaBetonaaaaselectedBeton = klasaBetonaComboBox.SelectedItem;
CalculateSomething(selectedBeton.Fck.ToString());
Sign up to request clarification or add additional context in comments.

Comments

1

It seems that you have used an IEnumerable<KlasaBetona> as datasource for the combobox. Now you're trying to cast the SelectedItem which is a single instance of KlaseBetona to string. That doesn't work since you haven't overloaded the explicit-conversion operator like this:

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:

KlasaBetona kl = (KlasaBetona) selectedBeton;
CalculateSomething(kl._Fck.ToString());

Comments

0

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. 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());

6 Comments

one more question i got... why now i get error here: var _dFck = Convert.ToDouble(fck.Text);
Because the value in there doesn't comply to your current regional settings format for decimal separated decimals. @antestipe
when i put textbox instead of label then it works. How can i set it up so the value can not be changed?
@antestipe - Are you converting back to a double in this same method? if you are then you would be much better to just pass a double into your method and then call ToString when setting the text. Thank you for the replies, Patrick
@antestipe: Search SO for similar problems. If that didn't work out: ask a new question.
|
0

In my case I got similar message Unable to cast object of type class to type Enum when I use JsonConverter to Serialize class.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.