Solved

Question about casting

Posted on 2014-03-11
3
364 Views
Last Modified: 2014-03-13
Experts, i found a few small bugs in my application and decided to turn Option Strict On to look for any other things. It gave me tons of stuff related to casting. I am wondering if i need to address everything it throws at me. Most everything was working before i turned Option Strict On. Do I need to worry about some of these things when it comes to compiling and distributing my application?


For example:
'Get the ID of the new company record.
Dim cmd as new oleDbCommand("SELECT @@IDENTITY",con)
Dim intID as integer = cmd.executeScalar
cmd.dispose

In this instance it does not like cmd.executeScalar and wants me to change it to:
Cint(cmd.executeScalar)

Just wondering how far i should go with casting.
0
Comment
Question by:Basicfarmer
3 Comments
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 250 total points
ID: 39921296
It's typically considered best practice to explicitly cast when you need to.
Remember that Option Strict On is basically Option Explicit + casting checks.
It errors when you implicitly cast in such a way that you lose precision (double to int, etc).
http://support.microsoft.com/kb/311329

If you really don't want it to check your casting, just use Option Explicit, but it is a good idea to pay attention to when you could be losing data by casting to a "smaller" type.
0
 
LVL 13

Accepted Solution

by:
Naman Goel earned 250 total points
ID: 39925980
see ExecuteScalar is returning object that's the reason it is giving this error. Actually it is just optional to worry about each and every thing like this, but just make sure the data coming from method is of the same data type as the variable to which it is assigned.
0
 

Author Closing Comment

by:Basicfarmer
ID: 39926548
Thanks for your responses, they have been helpful.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question