Invalid cast in code but not in command window

A

Andrew

Hi,

I am doing some code which involves type conversion and have run into a
problem I find odd.

I have a control of type class1 in a referenced assembly called
subclassedcontrols.
The line below used to work in other code, Facet is a property of my class1
control which inherits from a normal textbox
MessageBox.Show(CType(ctl, SubClassedControls.Class1).Facet & ctl.Text)

Exception message is:

An unhandled exception of type 'System.InvalidCastException' occurred in
VisualNotepad.exe

Additional information: Specified cast is not valid.

BUT this line executes OK in the command window

?CType(ctl, SubClassedControls.Class1).Facet & ctl.Text



Any pointers? I cannot understand why it should work in the command window
but not in the code running



Best regards,



Andrew
 
C

Chris Dunaway

The line below used to work in other code, Facet is a property of my class1
control which inherits from a normal textbox

Is Facet a string? Perhaps the MessageBox is expecting a string and cannot
cast Facet to a string. If it is descended from a textbox, perhaps you
could use Facet.Text
MessageBox.Show(CType(ctl, SubClassedControls.Class1).Facet & ctl.Text)

BUT this line executes OK in the command window

?CType(ctl, SubClassedControls.Class1).Facet & ctl.Text

The two lines are not strictly the same. One uses MessageBox. Perhaps
there is something about using messageBox that is causing the problems.
Any pointers? I cannot understand why it should work in the command window
but not in the code running

Good luck.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 

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