System.InvalidCastException

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have converted my program from vb6
In the initializecomponent event the following code give a n exception
CType(Me.controlname, System.ComponentModel.ISupportInitialize).EndInit()
Exception first-chance of type "System.InvalidCastException" in
system.windows.forms.dll

Additional information: Cast not supported.
what's wrong? I am using vsnet2002 with framework 1.0 sp3.
how can i take this exception away
 
DavideR said:
I have converted my program from vb6
In the initializecomponent event the following code give a n
exception CType(Me.controlname,
System.ComponentModel.ISupportInitialize).EndInit() Exception
first-chance of type "System.InvalidCastException" in
system.windows.forms.dll

Additional information: Cast not supported.
what's wrong? I am using vsnet2002 with framework 1.0 sp3.
how can i take this exception away



What is the type of Me.controlname? Which class(es) is it inherited from?


Armin
 
Armin,

Did you know that there is a simple solution to help with cast problems.

You should try it once, set Option Strict On in top of your program

:-)

Cor
 
Cor Ligthert said:
Armin,

Did you know that there is a simple solution to help with cast
problems.

You should try it once, set Option Strict On in top of your program

:-)


Ah, really? Good to know!

;-)


Armin
 
If you are going to use option strict (which is a VERY good idea) then
you may as well change it for the IDE as a whole under

Tools/Options/Projects

This will mean that all new projects will have option strict on.

Note:
Doing this will not change existing projects, you will need to change
those individually.


If for some reason need to turn it off for a class/module, you can put
Option Strict Off at the top of that particular file.


hth,
Alan.
 
If you are going to use option strict (which is a VERY good idea)
then you may as well change it for the IDE as a whole under

Tools/Options/Projects

This will mean that all new projects will have option strict on.

Note:
Doing this will not change existing projects, you will need to
change those individually.


If for some reason need to turn it off for a class/module, you can
put Option Strict Off at the top of that particular file.


Thanks a lot.

:-)


Armin
 
Alantolan,

Armin was famous about his messages about Option Strict On in past.

That was the reason I wrote it now.

:-)

Cor
 
I set Option Strict on but i still have the problem
the ocx in wich i experienced the problem are custom ocx but i use in other
form these ocx and i didn't have got the problem
 
David,

If you hoover over the function you see probably what is expected mostly you
can than solve your problem with

Ctype(myobject, WhatIsExpected)

Although Option Strict should warn that there is an invallid object,
therefore I think that is is wise to show that piece of code.

I hope this helps,

Cor
 
excuse me but i am a newbie in vb.net and i havn't understood
in the .begininit i don't have the problem.
how can i do?
 
David,

Sorry I was jumping in this thread in the middle, just because Armin did not
tell about Option Strict.

CType(Me.controlname, System.ComponentModel.ISupportInitialize).EndInit()

In know this statement as
CType(TheComControl, System.ComponentModel.ISupportInitialize).EndInit()

However I tried your statement, and that gives really a warning with option
Strict on.

Is there not something in your code as,

CType(Me.AxWebBrowser1,
System.ComponentModel.ISupportInitialize).BeginInit()

Than you know what it has to be

I hope this helps,

Cor
 
The problem is that every .endinit in the forms gives the same error and
there are controls used also in other forms who don't give the same problem
what can i do ?
thanks
 
David,

Without knowing what control it is that gives the problem, I am probably
absolute not able to help you. Not that I can help you as I know it, however
than we have more change.

Cor
 
is more then one control that giveme the problem
Some are custum control witten in vb6
other are buttons controls from MultimediaSoft (in other form this controls
don't have the problem. What can i do? delete and rewrite the code of the
form or problem raise again?
the error means that the control doesn't support Isupportinitialize?
 
David,

As I have this problem, I remove completly the control.

I add it than to the toolbar by rightclicking on that and than to check the
checkbox (be aware the tabpage let you choose from managed and com).

And than drag the control again to my form, than something generic is done,
from what I was not able to do it correctly by hand because probably a lag
of documentation or maybe lazyness (it is about the resx file).

I am not sure if it helps you however it is at least worth a try in my
opinion.

I hope this helps,

Cor
 

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

Back
Top