Solved

Question about casting

Posted on 2014-03-11
3
354 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

772 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