By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,271 Members | 1,352 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,271 IT Pros & Developers. It's quick & easy.

Late binding errors with Option Strict ?

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
17 Replies


P: n/a

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.

Nov 21 '05 #2

P: n/a
ng
Don't use option strict

Tom
David wrote:
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


Nov 21 '05 #3

P: n/a
"David" <in********************@yahoo.co.uk> wrote in message news:Oe**************@TK2MSFTNGP15.phx.gbl...
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


You need to cast 'sender' to the correct type.

CType(sender, MenuItem),Checked = Not CType(sender, MenuItem).Checked

--
Al Reid

Nov 21 '05 #4

P: n/a
"David" <in********************@yahoo.co.uk> schrieb:
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


\\\
Dim Item As MenuItem = DirectCast(sender, MenuItem)
Item.Checked = Not Item.Checked
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5

P: n/a
Herfried,

\\\
Dim Item As MenuItem = DirectCast(sender, MenuItem)
Item.Checked = Not Item.Checked
///

New one, from the German groups or by yourself.

:-)

Cor
Nov 21 '05 #6

P: n/a
"Cor Ligthert [MVP]" <no************@planet.nl> schrieb:
\\\
Dim Item As MenuItem = DirectCast(sender, MenuItem)
Item.Checked = Not Item.Checked
///

New one, from the German groups or by yourself.

:-)


Sorry, I don't follow you...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #7

P: n/a
"ng" <t_*****@yahoo.com> schrieb:
Don't use option strict


LOL!

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #8

P: n/a
Herfried,
Item.Checked = Not Item.Checked
///

New one, from the German groups or by yourself.

:-)


Sorry, I don't follow you...

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
Nov 21 '05 #9

P: n/a
Cor,

"Cor Ligthert [MVP]" <no************@planet.nl> schrieb:
Item.Checked = Not Item.Checked
///
New one, from the German groups or by yourself.

:-)


Sorry, I don't follow you...

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 ;-).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #10

P: n/a
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:uz**************@TK2MSFTNGP10.phx.gbl...
Herfried,
Item.Checked = Not Item.Checked
///
New one, from the German groups or by yourself.

:-)


Sorry, I don't follow you...

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.

--
Al Reid
Nov 21 '05 #11

P: n/a
Hi Herfried,

http://mindview.net/WebLog/log-0025

I'm still a great fan of Option Strict On, though ;-)

--

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
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> escribió en el mensaje
news:Oy****************@TK2MSFTNGP14.phx.gbl...
"ng" <t_*****@yahoo.com> schrieb:
Don't use option strict


LOL!

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #12

P: n/a
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
"Carlos J. Quintero [VB MVP]" <ca*****@NOSPAMsogecable.com> wrote in message
news:eu**************@TK2MSFTNGP14.phx.gbl...
| Hi Herfried,
|
| http://mindview.net/WebLog/log-0025
|
| I'm still a great fan of Option Strict On, though ;-)
|
| --
|
| 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
|
|
| "Herfried K. Wagner [MVP]" <hi***************@gmx.at> escribió en el
mensaje
| news:Oy****************@TK2MSFTNGP14.phx.gbl...
| > "ng" <t_*****@yahoo.com> schrieb:
| >> Don't use option strict
| >
| > LOL!
| >
| > --
| > M S Herfried K. Wagner
| > M V P <URL:http://dotnet.mvps.org/>
| > V B <URL:http://classicvb.org/petition/>
|
|
Nov 21 '05 #13

P: n/a
Carlos,
This just in ;-)

http://www.panopticoncentral.net/arc.../02/10428.aspx

Jay

"Carlos J. Quintero [VB MVP]" <ca*****@NOSPAMsogecable.com> wrote in message
news:eu**************@TK2MSFTNGP14.phx.gbl...
| Hi Herfried,
|
| http://mindview.net/WebLog/log-0025
|
| I'm still a great fan of Option Strict On, though ;-)
|
| --
|
| 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
|
|
| "Herfried K. Wagner [MVP]" <hi***************@gmx.at> escribió en el
mensaje
| news:Oy****************@TK2MSFTNGP14.phx.gbl...
| > "ng" <t_*****@yahoo.com> schrieb:
| >> Don't use option strict
| >
| > LOL!
| >
| > --
| > M S Herfried K. Wagner
| > M V P <URL:http://dotnet.mvps.org/>
| > V B <URL:http://classicvb.org/petition/>
|
|
Nov 21 '05 #14

P: n/a
> http://mindview.net/WebLog/log-0025

I'm still a great fan of Option Strict On, though ;-)


I mainly use option strict to catch ansi/unicode mismatches, otherwise,
I do agree. I think in vb it's different than C++, vb can be so "lazy" that option
strict is useful if only to remind oneself what is going on, especially if
one is migrating from vb6 to vb.net.
Nov 21 '05 #15

P: n/a
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]" <Ja************@tsbradley.net> escribió en
el mensaje news:ep**************@TK2MSFTNGP15.phx.gbl...
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


Nov 21 '05 #16

P: n/a
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/y...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

"Carlos J. Quintero [VB MVP]" <ca*****@NOSPAMsogecable.com> wrote in message
news:eW**************@TK2MSFTNGP09.phx.gbl...
|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]" <Ja************@tsbradley.net> escribió en
| el mensaje news:ep**************@TK2MSFTNGP15.phx.gbl...
| > 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
| >
|
Nov 21 '05 #17

P: n/a
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
Nov 21 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.