p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C# (http://p2p.wrox.com/forumdisplay.php?f=22)
-   -   invalid cast exception (http://p2p.wrox.com/showthread.php?t=3012)

giulio_santorini August 20th, 2003 07:08 PM

invalid cast exception
 
Hi,
I've got a little problem with my C# WinForms test application.
I would like to have a ComboBox showing a a visual value and some hidden values.
So I build a class able to store thoose values. This class called ListItem have got two private variable, one is a string containing the visual value, the

other one is a UDT variable. It is a struct called ValoriNascosti containing two variables a string and an int.
The costructor of ListItem store the three values (description, and the two of ValoriNascosti). There is two accessor methods that retrieve: the string of th

description; and another one retrieve a variable of type ValoriNascosti.

I create an ArrayList of ListItem in this way:
ArrayList elencoProdotti = new ArrayList();
elencoProdotti.Add(new ListItem("0001", 5, "Prodotto1"));

I populate the combo with the ArrayList:
this.comboBox1.DataSource = elencoProdotti;

Than I tell to the combo that the member to display is accessible from the method Descrizione:
comboBox1.DisplayMember = "Descrizione";
And the hidden value is accessible from the method AccessorValoriNascosti:
comboBox1.ValueMember = "AccessorValoriNascosti" ;

Thoose two are the accessors I was talking about.

When I run this I've got my dear combo with the visual data I've choose (like Prodotto1, Prodotto2, Prodotto3 etc...).

So I add code in the event SelectedIndexChanged, trying to show the hidden values when the combobox element changes. So I do this:
this.textBox1.Text = ((ValoriNascosti)this.comboBox1.SelectedValue).mod uli.ToString();
but I've got an invalid cast exception.
This line give the same error:
ValoriNascosti supporto = (ValoriNascosti)this.comboBox1.SelectedValue;


The value stored should be of valoriNascosti type... isn't right this way of casting?
I really don't have clues. :-Thanks for help.
Giulio


Olorin August 21st, 2003 06:44 AM

[buffled]
Neanch'io...
[/buffled]

che tipo di var e' "moduli" ?
Mi riferisco alla linea:
this.textBox1.Text = ((ValoriNascosti)this.comboBox1.SelectedValue).mod uli.ToString();

forse devi fare qualcosa tipo
this.textBox1.Text = (<classe di moduli>)(((ValoriNascosti)this.comboBox1.SelectedV alue).moduli).ToString();

ma non credo faccia molta differenza...

Sei sicuro che l'errore provenga da quella riga li'?
Dato che
ValoriNascosti supporto = (ValoriNascosti)this.comboBox1.SelectedValue;
da' lo stesso errore, io guarderei al SelectedValue del combo box...

..prova a fare un MessageBox nel SelectedIndexChanged event handler che ti lista i vari valori
del combo box (SelectedValue, SelectedItem, etc etc...

F.O.R.

Ankur_Verma August 21st, 2003 08:04 AM

This code works

//declaration of the struct and the class
    struct ValoriNascosti
    {
        public string valstring;
        public int valint;
    };

    class ListItem
    {
        private ValoriNascosti valone;
        private string Desc;

        public ListItem(string strVal,int intVal,string desc)
        {
            valone.valstring = strVal;
            valone.valint = intVal;
            Desc = desc;
        }

        public string getDesc
        {
            get
            {
                return Desc;
            }
        }

        public ValoriNascosti getValone
        {
            get
            {
                return valone;
            }
        }
    };

     .
     .
     .


//filling up
    ArrayList elencoProdotti = new ArrayList();
    elencoProdotti.Add(new ListItem("0001", 5, "Prodotto1"));
    elencoProdotti.Add(new ListItem("0002", 6, "Prodotto2"));
    elencoProdotti.Add(new ListItem("0003", 7, "Prodotto3"));

    this.testcombo.DataSource = elencoProdotti;
    this.testcombo.DisplayMember = "getDesc";
    this.testcombo.ValueMember = "getValone" ;




// usage
    MessageBox.Show(((ValoriNascosti)this.testcombo.Se lectedValue).valint.ToString());

I cannot point out the problem out of what you have described
in your query so just check this code to see if there is
anything I have done differently.

Regards

Ankur Verma
.Net and C++ Specialist
Wiley Tech Support


All times are GMT -4. The time now is 02:00 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.