InvalidCastException question

M

MounilK

Hi all,
I have a form which has a TabControl and a Button. At runtime, on the
TabControl, I create a TabPage with a TextBoxes. Now, when I click the
button after entering some text in the textbox, I want to show the text in a
messagebox. I tried the following code but get an error.

foreach (System.Windows.Forms.TextBox myTextBox in myTabPage.Controls)
{
MessageBox.Show (myTextBox.Text);
}

The error I get is:-

"An unhandled exception of type 'System.InvalidCastException' occurred in
MyLib.dll

Additional information: Specified cast is not valid."

How do I get this right?

TIA,
Mounil.
 
T

Tom Porterfield

Hi all,
I have a form which has a TabControl and a Button. At runtime, on the
TabControl, I create a TabPage with a TextBoxes. Now, when I click the
button after entering some text in the textbox, I want to show the text in a
messagebox. I tried the following code but get an error.

foreach (System.Windows.Forms.TextBox myTextBox in myTabPage.Controls)
{
MessageBox.Show (myTextBox.Text);
}

The error I get is:-

"An unhandled exception of type 'System.InvalidCastException' occurred in
MyLib.dll

Additional information: Specified cast is not valid."

How do I get this right?

foreach (Control ctrl in myTabPage.Controls)
{
TextBox myTextBox = ctrl as TextBox;
if (myTextBox != null)
MessageBox.Show (myTextBox.Text);
}
 
B

Bruce Wood

Your problem is this line:

foreach (System.Windows.Forms.TextBox myTextBox in myTabPage.Controls)

Several other programmers posting here have had the mistaken impression
that this means, "Loop through all of the TextBoxes on my TabPage." In
fact, it means "Loop through every control on my TabPage and attempt to
interpret every one of them as a TextBox."

If you have any control on your TabPage that is not a TextBox, the
foreach will bomb with an InvalidCastException, which is what you saw.

Tom Porterfield posted the correct solution.
 
M

MounilK

Hi Tom,
Thanks a lot for your help.
--Mounil.

Tom Porterfield said:
foreach (Control ctrl in myTabPage.Controls)
{
TextBox myTextBox = ctrl as TextBox;
if (myTextBox != null)
MessageBox.Show (myTextBox.Text);
}
 
J

Jeff Louie

foreach(Drawable d in drawableCollection)
{
System.Console.WriteLine(d.ToString());
}

Expands to:

System.Collections.IEnumerator enumerator= dc.GetEnumerator();
while (enumerator.MoveNext())
{

System.Console.WriteLine(((Drawable)(enumerator.Current)).ToString
());
}

with the potential for throwing a NullReferenceException or an
InvalidCastException.

Regards,
Jeff
 
J

Jeff Louie

Mounilk... you can think of foreach is as just a shorthand that the
compiler
understands and then "writes" code for you. so for your example:

foreach (System.Windows.Forms.TextBox myTextBox in myTabPage.Controls)
{
MessageBox.Show (myTextBox.Text);
}

you can think of the compiler rewriting your code to:

IEnumerator enumerator= myTabPage.Controls.GetEnumerator();
while (enumerator.MoveNext())
{
MessageBox.Show (((TextBox)enumerator.Current).Text);
}


There are two potential problems with this compiler "generated" code.
First is
the cast (TextBox)enumerator.Currrent. Unless every item in the
enumeration
is a TextBox, the cast will eventually fail since this is an "unsafe"
cast. This
will result in an InvalidCastException. The second problem is that an
item in
an enumeration may be null. You cannot safely invoke a method on a null
reference in C#. (Other languages let you send a message to a null
object by
the way.) This will result in a NullReferenceException. So you need to
do some
checking inside the foreach loop as Tom's code shows. I prefer to use is
because I think it is clearer coding.

An is expression evaluates to true if both of the following conditions
are met:
expression is not null.
expression can be cast to type. That is, a cast expression of the form
(type)(expression) will complete without throwing an exception.

So the code would look something like:

foreach (object o in myTabPage.Controls)
{
if (o is TextBox)
{
MessageBox.Show (((TextBox)o).Text);
}
// else do nothing and move on to next object
}

These pages may help:
http://www.geocities.com/Jeff_Louie/OOP/oop6.htm
http://www.geocities.com/Jeff_Louie/OOP/oop7.htm

Regards,
Jeff
 
M

MounilK

Hi Jeff,
Thanks a lot for making things so clear. Really appreciate your
help.
--Mounil.
 

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