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. 8 3785
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
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
"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
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 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?
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?
"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
Thanks a lot. I didn't know about DirectCast.
Nathan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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"...
|
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...
|
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...
|
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...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |