We help IT Professionals succeed at work.

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

Medium Priority
1,375 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

Comment
Watch Question

CERTIFIED EXPERT

Commented:
myMPI.NPI = Convert.ToInt32(patient.d.NPI.Value);


not sure that you need the conversion though

CERTIFIED EXPERT

Commented:
you might want to do this

if(patient.d.NPI.HasValue)
  myMPI.NPI = patient.d.NPI.Value;
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2009

Commented:
Will probably work if you add (int) in front of it (including brackets).
Top Expert 2009

Commented:
Ah, forget my comment, I didn't read well. Naspinski has better eyes here ;)
CERTIFIED EXPERT

Commented:
a little cleaner:
MyGlobalVars.docnpi = myMPI.NPI.GetValueOrDefault();

Open in new window

kwh3856Owner

Author

Commented:
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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.