[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)

Posted on 2009-02-18
7
Medium Priority
?
1,335 Views
Last Modified: 2013-12-17
I am not sure I understand this error message.  If two variable are already defined as integer why am I getting this error message.  I am not sure what it means.  Any help is greatly appreciated.

Here is my code where I get it.


// Global Variables Class 
        internal static class MyGlobalVars 
        {
            public static int docnpi;
            public static Guid initGuid;
        }
 
 
 
 
 
................down in the middle of my code I have................................
 myMPI.NPI = Convert.ToInt32(patient.d.NPI);
                MyGlobalVars.docnpi = myMPI.NPI;--------squiggly under the myMPI.NPI

Open in new window

0
Comment
Question by:kwh3856
7 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 23670382
myMPI.NPI = Convert.ToInt32(patient.d.NPI.Value);


not sure that you need the conversion though

0
 
LVL 22

Expert Comment

by:p_davis
ID: 23670386
you might want to do this

if(patient.d.NPI.HasValue)
  myMPI.NPI = patient.d.NPI.Value;
0
 
LVL 21

Accepted Solution

by:
naspinski earned 2000 total points
ID: 23670398
int? is a nullable integer, which means it can be null, or and integer.

int is just an integer, it can not be null.
MyGlobalVars.docnpi = myMPI.NPI != null ? (int)myMPI.NPI : 0;
 
//this will make it 0 if it is null, otherwise convert it to a non-nullable int

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 39

Expert Comment

by:abel
ID: 23670422
Will probably work if you add (int) in front of it (including brackets).
0
 
LVL 39

Expert Comment

by:abel
ID: 23670430
Ah, forget my comment, I didn't read well. Naspinski has better eyes here ;)
0
 
LVL 9

Expert Comment

by:Rob Siklos
ID: 23670534
a little cleaner:
MyGlobalVars.docnpi = myMPI.NPI.GetValueOrDefault();

Open in new window

0
 

Author Closing Comment

by:kwh3856
ID: 31548247
naspinski,
Thank you.  Your code worked just by pasting it in.  To each of you who chimed in, thank you.  Naspinski seemed to have the best solution for what I needed at this time.  Once again, thank you.
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month19 days, 19 hours left to enroll

872 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