invalid cast exception

G

Giulio Santorini

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).moduli.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
 
D

Doug Forster

Hi Giulio,

What you have shown us looks correct to me, but there must be something
wrong somewhere. Have you tried just setting this.comboBox1.SelectedValue
into an object variable and looking at its actual type in the debugger. If
that doesn't help then could you show us some more code such as the
declarations of ListItem and ValoriNascosti.

Cheers

Doug Forster
 
G

giulio santorini

Hi,
I've put the comboBox1.SelectedValue in an object variable
and in debug time, I've watch its type.

The application enter in the SelectedIndexChanged two
times before I got the control.
Thoose two times the type of comboBox1.SelectedValue is
ListItem. So is the entire class not just the
ValoriNascosti UDT.
The other times the type of comboBox1.SelectedValue is
ValoriNascosti.
I've generate a piece of code that handle a cast invalid
exception and retrieve the value I need via accessor for
the first two time, and via the public value for the other
times.
Like this:
try
{
this.textBox1.Text = ((ListItem)
this.comboBox1.SelectedValue).AccessorValoriNascosti.moduli
..ToString();
this.textBox2.Text = ((ListItem)
this.comboBox1.SelectedValue).AccessorValoriNascosti.codice
Arte;
}
catch(InvalidCastException)
{
this.textBox1.Text = ((ValoriNascosti)
this.comboBox1.SelectedValue).moduli.ToString();
this.textBox2.Text = ((ValoriNascosti)
this.comboBox1.SelectedValue).codiceArte;
}
catch
{
MessageBox.Show("Eccezione non gestita!");
}
Then it works. I've got what I need, but I still do not
understand why of the first two calls at
SelectedIndexChanged. And why the type is ListItem just
the first two times. I've add five values at the ArrayList.

I put there the declaration of ListItem.
It will be bad formatted I hope a cut and past will make
it back as I've got it in Visual Studio.
-----------------------------------------------------------
---------THE CODE BEGIN------------------------------------
-----------------------------------------------------------

public class ListItem
{
/// <summary>
/// Contiene la descrizione del prodotto,
che verrà visualizzata nel controllo
/// </summary>
private string descrizione;
/// <summary>
/// E'una UDT che contiene i valori che
associati alla descrizione formeranno
/// un elemento nel controllo.
/// </summary>
private ValoriNascosti valoriNascosti;

/// <summary>
/// Il costruttore si occupa di generare
un nuovo oggetto con i valori passati.
/// </summary>
/// <param name="codiceArte">codice
prodotto di arte elettrica</param>
/// <param name="moduli">numero moduli che
compongono il prodotto</param>
/// <param name="descrizione">descrizione
del prodotto</param>
public ListItem(string codiceArte, int
moduli, string descrizione)
{
this.valoriNascosti.codiceArte =
codiceArte;
this.valoriNascosti.moduli =
moduli;
this.descrizione = descrizione;
}

/// <summary>
/// Metodo accessor per ottenere la
variabile con i valori nascosti.
/// </summary>
public ValoriNascosti
AccessorValoriNascosti
{
get
{
return this.valoriNascosti;
}
}

/// <summary>
/// Metodo accessor al
valore "descrizione".
/// Restituisco il valore descrizione.
/// </summary>
public string Descrizione
{
get
{
return this.descrizione;
}
}

/// <summary>
/// Sovrascrivo il metodo restituendo i
valori che compongono l'oggetto.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return
this.valoriNascosti.codiceArte + ' ' +
this.valoriNascosti.moduli.ToString() + this.descrizione;
}

}

public struct ValoriNascosti
{
public string codiceArte;
public int moduli;
}
 
D

Doug Forster

Hi Giulio,

That's very strange, but I guess you have a workaround. I have only used
SelectedItem myself and that always seems to return the correct type
(ListItem in your case)

Cheers

Doug Forster

message Hi,
I've put the comboBox1.SelectedValue in an object variable
and in debug time, I've watch its type.

The application enter in the SelectedIndexChanged two
times before I got the control.
Thoose two times the type of comboBox1.SelectedValue is
ListItem. So is the entire class not just the
ValoriNascosti UDT.
The other times the type of comboBox1.SelectedValue is
ValoriNascosti.
I've generate a piece of code that handle a cast invalid
exception and retrieve the value I need via accessor for
the first two time, and via the public value for the other
times.
Like this:
try
{
this.textBox1.Text = ((ListItem)
this.comboBox1.SelectedValue).AccessorValoriNascosti.moduli
..ToString();
this.textBox2.Text = ((ListItem)
this.comboBox1.SelectedValue).AccessorValoriNascosti.codice
Arte;
}
catch(InvalidCastException)
{
this.textBox1.Text = ((ValoriNascosti)
this.comboBox1.SelectedValue).moduli.ToString();
this.textBox2.Text = ((ValoriNascosti)
this.comboBox1.SelectedValue).codiceArte;
}
catch
{
MessageBox.Show("Eccezione non gestita!");
}
Then it works. I've got what I need, but I still do not
understand why of the first two calls at
SelectedIndexChanged. And why the type is ListItem just
the first two times. I've add five values at the ArrayList.

I put there the declaration of ListItem.
It will be bad formatted I hope a cut and past will make
it back as I've got it in Visual Studio.
-----------------------------------------------------------
---------THE CODE BEGIN------------------------------------
-----------------------------------------------------------

public class ListItem
{
/// <summary>
/// Contiene la descrizione del prodotto,
che verrà visualizzata nel controllo
/// </summary>
private string descrizione;
/// <summary>
/// E'una UDT che contiene i valori che
associati alla descrizione formeranno
/// un elemento nel controllo.
/// </summary>
private ValoriNascosti valoriNascosti;

/// <summary>
/// Il costruttore si occupa di generare
un nuovo oggetto con i valori passati.
/// </summary>
/// <param name="codiceArte">codice
prodotto di arte elettrica</param>
/// <param name="moduli">numero moduli che
compongono il prodotto</param>
/// <param name="descrizione">descrizione
del prodotto</param>
public ListItem(string codiceArte, int
moduli, string descrizione)
{
this.valoriNascosti.codiceArte =
codiceArte;
this.valoriNascosti.moduli =
moduli;
this.descrizione = descrizione;
}

/// <summary>
/// Metodo accessor per ottenere la
variabile con i valori nascosti.
/// </summary>
public ValoriNascosti
AccessorValoriNascosti
{
get
{
return this.valoriNascosti;
}
}

/// <summary>
/// Metodo accessor al
valore "descrizione".
/// Restituisco il valore descrizione.
/// </summary>
public string Descrizione
{
get
{
return this.descrizione;
}
}

/// <summary>
/// Sovrascrivo il metodo restituendo i
valori che compongono l'oggetto.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return
this.valoriNascosti.codiceArte + ' ' +
this.valoriNascosti.moduli.ToString() + this.descrizione;
}

}

public struct ValoriNascosti
{
public string codiceArte;
public int moduli;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top