473,382 Members | 1,258 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Late binding errors with Option Strict ?

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
17 4451

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
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
"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
"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
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
"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
"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
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
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
"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
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
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
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
> 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: noone | last post by:
I like to write my code with optionstrict set to on, except for the late binding errors I get when there is a reason to change some value in a control on a postback, like: Label4.Text =...
1
by: Karl Lang | last post by:
Hi I've created a new configuration section in Web.Config to hold the connection string for my database. If I have Option Strict On I get a message "Option Strict On disallows late binding" when I...
5
by: eBob.com | last post by:
In another thread VJ made me aware of Tag. Fantastic! I've been wanting this capability for a long time. But it seems that I cannot use it with Option Strict On. In an event handler I have ......
30
by: lgbjr | last post by:
hi All, I've decided to use Options Strict ON in one of my apps and now I'm trying to fix a late binding issue. I have 5 integer arrays: dim IA1(500), IA2(500), IA3(500), IA4(500), IA5(500) as...
4
by: Heinz | last post by:
Hi all, I use VB.net 2003 and want to export data to Excel. Target PCs still have Office 2000 so I could not use Microsofts PIAs. Instead I use the included Excel 10 COM DLL from Microsoft....
6
by: Tim Roberts | last post by:
I've been doing COM a long time, but I've just come across a behavior with late binding that surprises me. VB and VBS are not my normal milieux, so I'm hoping someone can point me to a document...
4
by: Rippo | last post by:
Hi I have the following console application and am attempting to late bind a class with option strict on! However of course I cant and I get the following error "Option Strict On disallows late...
1
by: Adotek | last post by:
Hi All, I've just converted a solution from .Net v1.1 to v2.0, by allowing Visual Studio 2005 to do the conversion. Since doing so, I am getting a compilation error as follows: "Option...
2
by: GS | last post by:
I have installed the ms PIA for ofc XP, and followed the article http://support.microsoft.com/kb/247412/ trying to paste into a worksheet However I got late binding not allowed errors .......
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.