Solved

Question about casting

Posted on 2014-03-11
3
339 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for your responses, they have been helpful.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now