Option Strict On

  • Thread starter Thread starter Nathan
  • Start date Start date
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.
 
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.
 
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
 
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
 
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
 
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
 
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
 
Back
Top