Option Strict On

N

Nathan

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.
 
A

Armin Zingler

Nathan said:
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.
 
O

One Handed Man [ OHM# ]

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

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
 
O

One Handed Man [ OHM# ]

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

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



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

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

Nathan

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
 
C

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
 
A

Armin Zingler

Nathan said:
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
 

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

Similar Threads

option strict 5
Option Strict On 22
Option strict 4
Conversion to byte 3
Option Strict and .Save with an Image 4
Option Strict On and Ubound 4
Error when 'option strict' 2
Conversion issue with option strict 5

Top