473,382 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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).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
Nov 15 '05 #1
3 13860
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

"Giulio Santorini" <gi******************************@yahoo.it> wrote in
message news:07****************************@phx.gbl...
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

Nov 15 '05 #2
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).AccessorValoriNascos ti.moduli
..ToString();
this.textBox2.Text = ((ListItem)
this.comboBox1.SelectedValue).AccessorValoriNascos ti.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;
}

-----------------------------------------------------------
---------THE CODE IS END-----------------------------------
-----------------------------------------------------------

Thanks for your help,
Giulio
-----Original Message-----
Hi Giulio,

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

Cheers

Doug Forster

"Giulio Santorini" <gi******************************@yahoo.it> wrote inmessage news:07****************************@phx.gbl...
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

.

Nov 15 '05 #3
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

"giulio santorini" <gi****************************@yahoo.it> wrote in
message news:02****************************@phx.gbl...
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).AccessorValoriNascos ti.moduli
..ToString();
this.textBox2.Text = ((ListItem)
this.comboBox1.SelectedValue).AccessorValoriNascos ti.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;
}

-----------------------------------------------------------
---------THE CODE IS END-----------------------------------
-----------------------------------------------------------

Thanks for your help,
Giulio
-----Original Message-----
Hi Giulio,

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

Cheers

Doug Forster

"Giulio Santorini" <gi******************************@yahoo.it> wrote inmessage news:07****************************@phx.gbl...
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

.

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
9
by: buzz | last post by:
I am attempting to pass data between two asp.net web forms pages. I have found the method to do this on the msdn site here: ...
15
by: David | last post by:
Hi, I have built a web application that will be a very high profile application. We had tested it, demonstrated it and shown that it all works. On a dress rehearsal run through, it failed...
6
by: cs_hart | last post by:
I am getting an invalid cast exception - cast from string to type double is not valid. Dim curName As String Dim prevName As String = "" curName = CStr(rows.Item(i).Item(colSchName)) ' extract...
7
by: Chris Thunell | last post by:
I'm trying to loop through an exchange public folder contact list, get some information out of each item, and then put it into a vb.net datatable. I run though the code and all works fine until i...
4
by: Reticulated Ember | last post by:
I have the following code that fails with an invalid cast exception: .... System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage(); msg.BodyFormat = MailFormat.Html; ...
0
by: balukrishnan | last post by:
Hi... I am trying to create an object of Shdocvw.InternetExplorer class, using which I need to launch a particular URL(A JSP Webpage). Then I need to get the document of the page loaded. But...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.