Late binding errors with Option Strict ?

D

David

Hi all,

I have the following problem: my program works fine, but when I add option
strict at the top of the form, the following sub fails with an error that option
strict does not allow late binding. What should I do?


Public Sub MyMnuHandler(ByVal sender As Object, ByVal e As System.EventArgs)

If sender.checked = True Then sender.checked = False Else sender.checked = True

End Sub
 
A

alantolan

Option Strict ON is a great idea but will involve a touch more work.


You need to cast the sender to the appropriate type

Try

dim src as MenuItem = CType(sender, MenuItem)
src.checked = not src.checked


hth,
Alan.
 
H

Herfried K. Wagner [MVP]

Cor,

Cor Ligthert said:
That = Not item.Checked is something I never saw in these newsgroups.

Is it something you come with or did you see it in a German newsgroup.

Mhm... I use this for some years now ;-).
 
A

Al Reid

Cor Ligthert said:
Herfried,

That = Not item.Checked is something I never saw in these newsgroups.

Is it something you come with or did you see it in a German newsgroup.

Cor

Cor,

I've been using that for years in VB Classic. It's a fairly old bethod of
toggling booleans. I posted the same approach just minutes before
Herfried's post.
 
C

Carlos J. Quintero [VB MVP]

I never use Option Strict Off, even dealing with COM Interop, I use
Reflection if I need to use late bound calls, even if it takes more code.
Maybe I would choose Option Strict Off if I could apply that option only to
a region of code rather than to the whole file...

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
J

Jay B. Harlow [MVP - Outlook]

Carlos,
I don't use it for late bound calls, as much as eliminating the need for
downcasts.

Instead of Reflection, have you considered using CallByName?

When I use it (Option Strict Off) I limit it to as few as methods as
possible in as small a class as possible, which means I tend to have an
extra light class that is Option Strict Off. Also I will turn Option Strict
On to help eliminate any obvious errors, then turn Option Strict Off to run
the code. Then depending on the usage of the Option Strict Off class, I will
refactor it inline into the original class... Possibly eliminating the
Option Strict Off class.

As I stated I normally use Option Strict Off to encapsulate downcasts, which
I can easily add inline, however all the downcasts can make the code harder
to read. Hence the separate class to get "things working" then use the
Refactoring to improve the code.

| Maybe I would choose Option Strict Off if I could apply that option only
to
| a region of code rather than to the whole file...
As I suggested VB 2005's partial classes will simplify this (using Option
Strict Off), as I can put all the Option Strict Off stuff in one source
member & have it be a partial class of a larger class that is Option Strict
On. Eliminating the need for 2 classes...

http://msdn2.microsoft.com/library/yfzd5350(en-us,vs.80).aspx

Something like:

' Something.Interop.vb
Option Strict Off

Partial Class Something

Public Sub DoSomeLateBinding()
...
End Sub

Public Function EncapsulateDowncast() As SomethingSpecific
...
End Sub

End Class

' Something.vb
Option Strict On

Partial Class Something
' the majority of the class definition

Public Sub DoSomething()
DoSomeLateBinding()
Dim x As SomethingSpecific = EncapsulateDowncast()
End Sub

End Class

Notice how the Something class's definition is split between the
Something.Interop.vb & Something.vb files. The Something.Interop.vb contains
implicit downcasts & late bound code, while Something.vb contains early
bound code & explicit downcasts.

Hope this helps
Jay

|I never use Option Strict Off, even dealing with COM Interop, I use
| Reflection if I need to use late bound calls, even if it takes more code.
| Maybe I would choose Option Strict Off if I could apply that option only
to
| a region of code rather than to the whole file...
|
| --
|
| Best regards,
|
| Carlos J. Quintero
|
| MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
| You can code, design and document much faster.
| Free resources for add-in developers:
| http://www.mztools.com
|
| "Jay B. Harlow [MVP - Outlook]" <[email protected]> escribió en
| el mensaje | > Carlos,
| > I normally run with Option Strict On, however Option Strict Off is very
| > useful some times. For example when dealing with COM interop, especially
| > COM
| > objects written for VBScript where all return values were defined as
| > Variant...
| >
| > I'm looking forward to Partial Classes in VB 2005 as they will allow me
to
| > isolate just the parts of my class that needs Option Strict Off to its
own
| > source file!
| >
| > Jay
| >
|
 
C

Carlos J. Quintero [VB MVP]

Hi Jay,
Instead of Reflection, have you considered using CallByName?

I'm not sure if I was aware of that function when I wrote the code some
years ago...today surely I would use CallByName.
As I suggested VB 2005's partial classes will simplify this (using Option
Strict Off), as I can put all the Option Strict Off stuff in one source
member & have it be a partial class of a larger class that is Option
Strict
On. Eliminating the need for 2 classes...

I still find unfortunate that Option Strict must be applied at file level (a
physical entity) and not at code level (a logical entity). Partial classes
allow you to have a single class (a great advance, certainly), but you are
still forced to have 2 files only for this purpose...

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 

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