473,396 Members | 1,827 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,396 software developers and data experts.

Option Strict

YYZ
After reading many messages in this group, it seems that the preferred
setting for this is ON. Okay, I did that in my project (first with
..Net -- long time VB6 developer) and now a bunch of problems have
cropped up. Most are easy to solve with using an explicit type cast
cType(whatever, whatever type), but some aren't.

Situation 1: I've got code that tries to get the itemkey of the
selected item in a combobox.
cmbDecCBrwrM1.SelectedItem.ItemKey

That worked before, but now, "Option Strict On disallows late binding"
-- well, I'm not sure how to get the item key of the selected item,
because now selecteditem only has the "GetType" default method in its
intellisense. I'm not sure what I could type this to to get an itemkey
property...

Situation 2: I've created a collection class (stolen, actually, from
Microsoft I believe) that allows me to have a strongly typed
colleciton. I solved a couple of problems by overloading the remove
method to handle an integer or a string, and also Type-ing some other
things...but here is what is left:

Public ReadOnly Property NewEnum() As stdole.IUnknown
Get
'this property allows you to enumerate
'this collection with the For...Each syntax
NewEnum = mCol.GetEnumerator
End Get
End Property

Error is on the NewEnum = mCol.GetEnumerator line, and the error is
that it can't convert iUnknown to iEnumerator. I think I solved this
one by changing the declaration of the proc to ... As
System.Collections.IEnumerator But I don't know if that messed
everything up because I can't run the project to test it because of the
other errors.

Finally, is using Option Strict On really that good of an idea if all
you do is do some explicit CType functions? I mean, if, while running
your program, it blows up on something without Option Strict On, it
wouldn't help to have Option Strict on and just CType that same line,
because you are still "going out on a limb" and trusting that the
object you are CTyping really IS the type of object you want...right?

Any pointers would be appreciated.

Matt

Jan 11 '06 #1
9 2287
Is the ItemKey property declared as Object? In which case you have to use
CType again to cast it to the appropriate type.

Option Strict on is a good idea. If you were using C#, it would be as if
this was on all the time, since C# has no concept of Option Strict. Option
Strict forces you to think about what are the types of the objects you are
dealing with, and catches many many errors at compile time instead of at run
time.

Yes, a CType can fail and you can still have a runtime error. But, when you
are writing that CType code, it really forces you to think about the type of
the object you are CType'ing. So odds are you are not going to try to CType
it to the wrong type.

Additionally, having Option Strict On should result in more efficient code.
The compiler can know what types your objects are, and emit more efficient
IL. Whereas the other way, all that takes place at runtime, and the runtime
is trying to figure out what type everything is, and if this method really
exists, etc.

To me, turning it off, is just being lazy. I'm sure there are situations
where it is useful or necessary, but that would be a pretty rare scenario.

"YYZ" <ma********@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
After reading many messages in this group, it seems that the preferred
setting for this is ON. Okay, I did that in my project (first with
.Net -- long time VB6 developer) and now a bunch of problems have
cropped up. Most are easy to solve with using an explicit type cast
cType(whatever, whatever type), but some aren't.

Situation 1: I've got code that tries to get the itemkey of the
selected item in a combobox.
cmbDecCBrwrM1.SelectedItem.ItemKey

That worked before, but now, "Option Strict On disallows late binding"
-- well, I'm not sure how to get the item key of the selected item,
because now selecteditem only has the "GetType" default method in its
intellisense. I'm not sure what I could type this to to get an itemkey
property...

Situation 2: I've created a collection class (stolen, actually, from
Microsoft I believe) that allows me to have a strongly typed
colleciton. I solved a couple of problems by overloading the remove
method to handle an integer or a string, and also Type-ing some other
things...but here is what is left:

Public ReadOnly Property NewEnum() As stdole.IUnknown
Get
'this property allows you to enumerate
'this collection with the For...Each syntax
NewEnum = mCol.GetEnumerator
End Get
End Property

Error is on the NewEnum = mCol.GetEnumerator line, and the error is
that it can't convert iUnknown to iEnumerator. I think I solved this
one by changing the declaration of the proc to ... As
System.Collections.IEnumerator But I don't know if that messed
everything up because I can't run the project to test it because of the
other errors.

Finally, is using Option Strict On really that good of an idea if all
you do is do some explicit CType functions? I mean, if, while running
your program, it blows up on something without Option Strict On, it
wouldn't help to have Option Strict on and just CType that same line,
because you are still "going out on a limb" and trusting that the
object you are CTyping really IS the type of object you want...right?

Any pointers would be appreciated.

Matt

Jan 11 '06 #2
YYZ
Okay, thanks for that. I hadn't looked @ this code in a while and had
just assumed that I was adding strings into the combobox. Actually,
they were my own clsListItem objects, and those did have an ItemKey
property -- so using cType again worked perfectly -- thanks for that!

Finally, thanks for the reason WHY to use Option Strict. That really
helped. I didn't realize it would end up being faster, which in .Net
is always a good thing.

I feel better about using CType, which for a little while there was
making me feel like I was just hiding a bunch of problems instead of
fixing them!

I honestly think this will help me figure out how to use .Net better
now because I'll have to learn the object hierarchy better.

Matt

Jan 11 '06 #3
You bring up a good point regarding object hierarchy.

When you cast to the correct type, you will also end up getting VS.NET to
show you the intellisense for that object. Not only will this help you code
faster, you will learn a lot more about the object and what it can do by
seeing its properties/methods.

"YYZ" <ma********@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Okay, thanks for that. I hadn't looked @ this code in a while and had
just assumed that I was adding strings into the combobox. Actually,
they were my own clsListItem objects, and those did have an ItemKey
property -- so using cType again worked perfectly -- thanks for that!

Finally, thanks for the reason WHY to use Option Strict. That really
helped. I didn't realize it would end up being faster, which in .Net
is always a good thing.

I feel better about using CType, which for a little while there was
making me feel like I was just hiding a bunch of problems instead of
fixing them!

I honestly think this will help me figure out how to use .Net better
now because I'll have to learn the object hierarchy better.

Matt

Jan 11 '06 #4
"YYZ" <ma********@gmail.com> schrieb
After reading many messages in this group, it seems that the
preferred setting for this is ON. Okay, I did that in my project
(first with .Net -- long time VB6 developer) and now a bunch of
problems have cropped up. Most are easy to solve with using an
explicit type cast cType(whatever, whatever type), but some aren't.

Situation 1: I've got code that tries to get the itemkey of the
selected item in a combobox.
cmbDecCBrwrM1.SelectedItem.ItemKey

That worked before, but now, "Option Strict On disallows late
binding" -- well, I'm not sure how to get the item key of the
selected item, because now selecteditem only has the "GetType"
default method in its intellisense. I'm not sure what I could type
this to to get an itemkey property...
What is the type of the item in the combobox?

Situation 2: I've created a collection class (stolen, actually, from
Microsoft I believe) that allows me to have a strongly typed
colleciton. I solved a couple of problems by overloading the remove
method to handle an integer or a string, and also Type-ing some other
things...but here is what is left:

Public ReadOnly Property NewEnum() As stdole.IUnknown
Get
'this property allows you to enumerate
'this collection with the For...Each syntax
NewEnum = mCol.GetEnumerator
End Get
End Property

Error is on the NewEnum = mCol.GetEnumerator line, and the error is
that it can't convert iUnknown to iEnumerator. I think I solved this
one by changing the declaration of the proc to ... As
System.Collections.IEnumerator But I don't know if that messed
everything up because I can't run the project to test it because of the
other errors.
That's ok. The usual way is implementing IEnumerable and return
mCol.GetEnumerator in GetEnumerator, while "GetEnumerator Implements
IEnumerable.GetEnumerator".

Finally, is using Option Strict On really that good of an idea if all
you do is do some explicit CType functions? I mean, if, while running
your program, it blows up on something without Option Strict On, it
wouldn't help to have Option Strict on and just CType that same line,
because you are still "going out on a limb" and trusting that the
object you are CTyping really IS the type of object you want...right?

Option Strict Off: runtime error can occur due to wrong explicit casts,
/and/ due to wrong implicit casts or other late binding errors.

Option Strict On: runtime errors can occur due to wrong explicit cast, /not/
due to wrong implicit casts or other late binding errors.
Why enable additional risks by turning Option Strict Off? In a professional
development environment this is usually considered unacceptable - apart from
cases where late binding is recommended, like when being able to use
different office versions for automation - because a main goal is to prevent
and detect faults ASAP.
Armin

Jan 11 '06 #5
YYZ
> What is the type of the item in the combobox?

My bad -- still stuck in VB6 -- I coded this combobox a while ago, and
really forgot that all list items have to be an object...so I thought
that ItemKey was a built-in property of the selecteditem property, and
couldn't understand where it went when I turned Option Explicit on.
Situation 2: I've created a collection class (stolen, actually, from
Microsoft I believe) that allows me to have a strongly typed
colleciton. I solved a couple of problems by overloading the remove
method to handle an integer or a string, and also Type-ing some other
things...but here is what is left:
Public ReadOnly Property NewEnum() As stdole.IUnknown
Get
'this property allows you to enumerate
'this collection with the For...Each syntax
NewEnum = mCol.GetEnumerator
End Get
End Property

Error is on the NewEnum = mCol.GetEnumerator line, and the error is
that it can't convert iUnknown to iEnumerator. I think I solved this
one by changing the declaration of the proc to ... As
System.Collections.IEnumerator But I don't know if that messed
everything up because I can't run the project to test it because of the
other errors.


That's ok. The usual way is implementing IEnumerable and return
mCol.GetEnumerator in GetEnumerator, while "GetEnumerator Implements
IEnumerable.GetEnumerator".


Glad to know it will work, and it did after I fixed the other errors
thanks to you and to Marina. However, I honestly never had to learn
the interfaces for com objects, so IEnumberable and IUknown and the
like are still greek to me. Is the way that I'm doing it "incorrect"?
Is there a better way to do it?
Finally, is using Option Strict On really that good of an idea if all
you do is do some explicit CType functions? I mean, if, while running
your program, it blows up on something without Option Strict On, it
wouldn't help to have Option Strict on and just CType that same line,
because you are still "going out on a limb" and trusting that the
object you are CTyping really IS the type of object you want...right?


Option Strict Off: runtime error can occur due to wrong explicit casts,
/and/ due to wrong implicit casts or other late binding errors.

Option Strict On: runtime errors can occur due to wrong explicit cast, /not/
due to wrong implicit casts or other late binding errors.


That makes sense.
Why enable additional risks by turning Option Strict Off? In a professional
No reason at all. I think it was turned off when I got .Net, or maybe
I did while still learning the IDE.
development environment this is usually considered unacceptable - apart from
I am coding in a vacuum, unfortunatley. Just left a larger developer
shop and I'm the lone programmer among a bunch of sales guys doing a
new system for them with a new tool. All the tribal knowledge that
most people get from other co-workers isn't available to me, hence why
I'm spending a couple hours a day on the groups reading old posts.
cases where late binding is recommended, like when being able to use
different office versions for automation - because a main goal is to prevent
and detect faults ASAP.


Absolutely. I'll keep it on from now on. It only took me an hour or
two with the help I received from the group to convert my project to
using Option Strict. Glad I did it NOW instead of in 6 months when I
have 40000 lines of code to go through.

Matt

Jan 11 '06 #6
In case you don't know it, Directcast is faster than Ctype but the object
type must be the same as what you cast it to in DirectCast since it won't
convert, i.e.,

dim s as Object = "1234"
Ctype(s,integer) will work, Directcast(s,integer) won't.
--
Dennis in Houston
"YYZ" wrote:
Okay, thanks for that. I hadn't looked @ this code in a while and had
just assumed that I was adding strings into the combobox. Actually,
they were my own clsListItem objects, and those did have an ItemKey
property -- so using cType again worked perfectly -- thanks for that!

Finally, thanks for the reason WHY to use Option Strict. That really
helped. I didn't realize it would end up being faster, which in .Net
is always a good thing.

I feel better about using CType, which for a little while there was
making me feel like I was just hiding a bunch of problems instead of
fixing them!

I honestly think this will help me figure out how to use .Net better
now because I'll have to learn the object hierarchy better.

Matt

Jan 12 '06 #7
YYZ,

As small addition to the fine texts you have got.

Try to avoid using the raw type object. (by instance for passing)

I see that often done by VB6 programmers, try to use the real type.

With that there is much less casting/converting needed.

Cor
Jan 12 '06 #8
"YYZ" <ma********@gmail.com> schrieb

Had machine problems yesterday, so I couldn't answer earlier.
That's ok. The usual way is implementing IEnumerable and return
mCol.GetEnumerator in GetEnumerator, while "GetEnumerator
Implements IEnumerable.GetEnumerator".
Glad to know it will work, and it did after I fixed the other errors
thanks to you and to Marina. However, I honestly never had to learn
the interfaces for com objects, so IEnumberable and IUknown and the
like are still greek to me. Is the way that I'm doing it
"incorrect"? Is there a better way to do it?


Pardon, better than...?
development environment this is usually considered unacceptable -
apart from


I am coding in a vacuum, unfortunatley. Just left a larger
developer shop and I'm the lone programmer among a bunch of sales
guys


I'm a lone warrior, too (and I'm also the sales guy). :) (one hint:
*never* do what sales guys say ("just this little tiny additional
feature...") ;-) - but you probably know this already)
doing a new system for them with a new tool. All the tribal
knowledge that most people get from other co-workers isn't available
to me, hence why I'm spending a couple hours a day on the groups
reading old posts.


I'm also learning a lot from here. Sometimes by asking, and much more by
answering. Well, I've learned where the table of contents is... ;)
cases where late binding is recommended, like when being able to
use different office versions for automation - because a main goal
is to prevent and detect faults ASAP.


Absolutely. I'll keep it on from now on. It only took me an hour
or two with the help I received from the group to convert my project
to using Option Strict. Glad I did it NOW instead of in 6 months
when I have 40000 lines of code to go through.


Good decision. :-)
Armin

Jan 12 '06 #9
YYZ
Interesting. I'll try to figure out when I can use that and when I
can't...

Matt

Jan 12 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Microsoft News | last post by:
I have a project that was created all with Option Strict OFF. Works great, not a problem with it. But if I turn Option Strict ON then I get a LOT of errors. My question, should I even care...
11
by: Daylor | last post by:
hi. im using option strict on. im doing in ,from the simple reason ,to be warn when there are implict conversion like string to int ,int to string. BUT. the price ,(now i see ), is very bad....
8
by: Rich | last post by:
Hello, If I leave Option Strict Off I can use the following syntax to read data from a Lotus Notes application (a NotesViewEntry object represents a row of data from a Lotus Notes View - like a...
17
by: David | last post by:
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...
15
by: guy | last post by:
when i first started using .net (beta 1) i came across option strict and thought hey this could be really good, and since then have always turned it on, most people here seem to agree that this is...
13
by: C. Moya | last post by:
I fully expected the lack of a way to set Option Strict globally to be fixed in SP1. I can't seem to figure out if it has been fixed or not. It still seems we have to add the declaration at the top...
1
by: Jerad Rose | last post by:
I believe this issue is specific to ASP.NET. Why does VB.NET (2.0) ignore the project-level setting for Option Strict? I have the setting turned on in web.config: <compilation debug="true"...
18
by: Poldie | last post by:
How do I turn it on? I'm using vb 2005 in visual studio 2005 sp1. In my web.config I have: <compilation debug="true" strict="true" /> In my Tools/Options/Projects and solutions/vb defaults...
8
by: Rory Becker | last post by:
A wise man once said: "Never put off until runtime what you can fix at compile time." Actually I think he said it about 10 minutes before I started this post. I am a firm believer, like the...
8
by: =?Utf-8?B?R3JlZw==?= | last post by:
We have an application in our office that has the Option Strict option set to off right now. I do understand it should be set to ON, but right now, I'm just going to continue with it this way since...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.