Solved

Question about casting

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I use SQL Stored Procedure to return FedEx Zone 17 41
Problem to error 4 46
Excel file not created as expected 7 50
Entity Framework 7 32
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

867 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

16 Experts available now in Live!

Get 1:1 Help Now