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

Option Strict On

P: n/a
Hi,

I have an operation in which I divide one integer by another and assign the
result to a third integer. However, I get the error message: "Option Strict
On disallows implicit conversions from 'Double' to 'Integer'." I know this
can be avoided by using CInt; however, what would be the affect in how my
code works if I turn Option Strict off?--the effect on converting double to
integer as well as on other operations?

Thanks.
Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Option Strict helps prevent you from writing code which is likely to give
you runtime errors. It does this by making sure that casting is well defined
and permissable. It also helps you to prevent data loss between numeric
types.

In your example. A integer dividion will result in a double potentially, so
it tries to implicitly convert to just that. Because you had OSTRICT = ON ,
the compiler disallowed this. If you really want the result in the integer,
then use CType( Int1/Int2, Integer ).

Example.
'Option Strict is ON
Dim int1, int2, int3 As Integer
int1 = 2
int2 = 3

'Casts Result of division to Integer
int3 = CType(int1 / int2, Integer)
MessageBox.Show(int3.ToString())

'Casts Result of division to Integer
MessageBox.Show(CType(int1 / int2, Double).ToString())

My advice, leave it on.

Regards - OHM
Nathan wrote:
Hi,

I have an operation in which I divide one integer by another and
assign the result to a third integer. However, I get the error
message: "Option Strict On disallows implicit conversions from
'Double' to 'Integer'." I know this can be avoided by using CInt;
however, what would be the affect in how my code works if I turn
Option Strict off?--the effect on converting double to integer as
well as on other operations?

Thanks.


Regards - OHM# OneHandedMan{at}BTInternet{dot}com
Nov 20 '05 #2

P: n/a

Second bit should read

'Casts Result of division to ((Double))
MessageBox.Show(CType(int1 / int2, Double).ToString())

Not

'Casts Result of division to ((Integer))
MessageBox.Show(CType(int1 / int2, Double).ToString())

Regards - OHM
One Handed Man [ OHM# ] wrote:
Option Strict helps prevent you from writing code which is likely to
give you runtime errors. It does this by making sure that casting is
well defined and permissable. It also helps you to prevent data loss
between numeric types.

In your example. A integer dividion will result in a double
potentially, so it tries to implicitly convert to just that. Because
you had OSTRICT = ON , the compiler disallowed this. If you really
want the result in the integer, then use CType( Int1/Int2, Integer ).

Example.
'Option Strict is ON
Dim int1, int2, int3 As Integer
int1 = 2
int2 = 3

'Casts Result of division to Integer
int3 = CType(int1 / int2, Integer)
MessageBox.Show(int3.ToString())

'Casts Result of division to Integer
MessageBox.Show(CType(int1 / int2, Double).ToString())

My advice, leave it on.

Regards - OHM
Nathan wrote:
Hi,

I have an operation in which I divide one integer by another and
assign the result to a third integer. However, I get the error
message: "Option Strict On disallows implicit conversions from
'Double' to 'Integer'." I know this can be avoided by using CInt;
however, what would be the affect in how my code works if I turn
Option Strict off?--the effect on converting double to integer as
well as on other operations?

Thanks.


Regards - OHM# OneHandedMan{at}BTInternet{dot}com


Regards - OHM# OneHandedMan{at}BTInternet{dot}com
Nov 20 '05 #3

P: n/a
"Nathan" <nk*********************@softhome.net> schrieb

I have an operation in which I divide one integer by another and
assign the result to a third integer. However, I get the error
message: "Option Strict On disallows implicit conversions from
'Double' to 'Integer'." I know this can be avoided by using CInt;
however, what would be the affect in how my code works if I turn
Option Strict off?--the effect on converting double to integer as
well as on other operations?


Why not try it? If you use "\" as integer division operator, the error
disappears - even with option strict on. Using "/", the floating point
divison operator, both values are converted to doubles, then divided, then
assigned to the variable. Beep, error => result must be converted. Wihtout
option strict - hmm, I don't know. If I wanted to do a conversion, I'd
convert it explicitly. Otherwise the compiler can not know whether the
conversion is intended or not.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #4

P: n/a
Thanks for the help.

I've run into another option strict problem now. I have an event handler
(quite a few for different forms, actually) that handles the click event for
a number of buttons. In the subroutine, I want to access the .Tag property
of the button that was click. If I use sender.Tag, I get a late binding
error. I've tried changing System.Object in the sub's paramater to Button,
but then I get an error saying the signature is incorrect. How do I work
this out?

Thanks
Nov 20 '05 #5

P: n/a
Cor
Hi Nathan,

In addition to the others,

Your program will be much slower, that is when people say C# is faster.
With C# option Strict is always on.

When option Strict is on in VB.net, than C# and VB.net have the same
performance.

Cor

I have an operation in which I divide one integer by another and assign the result to a third integer. However, I get the error message: "Option Strict On disallows implicit conversions from 'Double' to 'Integer'." I know this can be avoided by using CInt; however, what would be the affect in how my
code works if I turn Option Strict off?--the effect on converting double to integer as well as on other operations?

Nov 20 '05 #6

P: n/a
Cor
Nathan,

Normaly with

Directcast(sender, button).xxx = myvariable

Cor

I've run into another option strict problem now. I have an event handler
(quite a few for different forms, actually) that handles the click event for a number of buttons. In the subroutine, I want to access the .Tag property of the button that was click. If I use sender.Tag, I get a late binding
error. I've tried changing System.Object in the sub's paramater to Button, but then I get an error saying the signature is incorrect. How do I work
this out?

Nov 20 '05 #7

P: n/a
"Nathan" <nk*********************@softhome.net> schrieb
Thanks for the help.

I've run into another option strict problem now. I have an event
handler (quite a few for different forms, actually) that handles the
click event for a number of buttons. In the subroutine, I want to
access the .Tag property of the button that was click. If I use
sender.Tag, I get a late binding error.
You get it because not every object has a Tag property.
I've tried changing
System.Object in the sub's paramater to Button, but then I get an
error saying the signature is incorrect. How do I work this out?


As you know the type of the sender, you can type-cast the reference:

dim tag as object

tag = directcast(sender, button).tag
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
Nov 20 '05 #8

P: n/a
Thanks a lot. I didn't know about DirectCast.

Nathan
Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.