?
Solved

Question about casting

Posted on 2014-03-11
3
Medium Priority
?
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 1000 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 1000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

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