Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 20th, 2003, 07:08 PM
Registered User
Join Date: Aug 2003
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default invalid cast exception

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.

Reply With Quote
  #2 (permalink)  
Old August 21st, 2003, 06:44 AM
Authorized User
Join Date: Jun 2003
Location: Gainesville, FL, USA.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts


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

Reply With Quote
  #3 (permalink)  
Old August 21st, 2003, 08:04 AM
Friend of Wrox
Join Date: Jun 2003
Location: , , .
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma

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
                return Desc;

        public ValoriNascosti getValone
                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.


Ankur Verma
.Net and C++ Specialist
Wiley Tech Support
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting Invalid Class Exception in tomcat rnilay81 J2EE 1 February 21st, 2007 06:30 AM
random "invalid group condition" exception hannah_h Crystal Reports 1 January 23rd, 2007 02:25 PM
Specified cast is not valid. abstar BOOK: ASP.NET Website Programming Problem-Design-Solution 2 March 31st, 2005 03:00 PM
Invalid cast in MyAccount.aspx carinlindberg BOOK: ASP.NET Website Programming Problem-Design-Solution 6 January 15th, 2005 11:49 PM
"Specified cast is not valid" help BaBaBooey ASP.NET 1.0 and 1.1 Basics 2 November 23rd, 2004 12:12 PM

All times are GMT -4. The time now is 04:37 AM.

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