Craig Key
Craig Key

Reputation: 151

Problems with converting type int? to int. Missing a cast?

I am trying to pull a date from a separate program and use it to determine the expiration dates of contracts. I have called int's for the month day and year and then used DateTime to assign to another var.

I am fairly new to C# and and can't find a work around for the error I get with this block. The errors tell me that it cannot implicitly convert int? to int, when I use an int? var to declare the day month and year it then shifts the error down to the DateTime line.

This probably needs to be structured differently but I can't figure out what that way would be.

private Instrument m_instr = null;

private void m_getInstrDetails(Instrument instr)
{
    m_ContractName = instr.Name.ToString();
    m_type = instr.Product.Type.ToString();
    m_prod = instr.Product.ToString();
    m_SmallestTickIncrtement = instr.InstrumentDetails.SmallestTickIncrement;


    //month calc
    int month = m_instr.InstrumentDetails.ExpirationDate.Month; 
    int day = m_instr.InstrumentDetails.ExpirationDate.Day;
    int year = m_instr.InstrumentDetails.ExpirationDate.Year;

    m_expDate1 = new DateTime(year, month, day);

Upvotes: 0

Views: 872

Answers (3)

Rion Williams
Rion Williams

Reputation: 76547

A nullable integer (int?) cannot be cast to a traditional integer (int) since there are scenarios where one might be null, and you'll need to determine how you want to handle such occasions.

Consider Parsing or Using a Default Value

You need to determine what you want to occur when your nullable value is null. Nullable integers by default expose a HasValue property, which you can use to determine as a default or you could consider using the null-propagation operator to handle this :

// This will use the expiration date if it exists, otherwise it will use 1
int month = m_instr.InstrumentDetails.ExpirationDate?.Month ?? 1;

Another option involves setting an initial value and using the int.TryParse() method to update the value prior to use :

int month = 1;
int.TryParse(m_instr.InstrumentDetails.ExpirationDate?.Month, out month);

Considering Throwing an Exception

If you don't want to allow these types of scenarios to occur and using some default value isn't feasible, you could consider just throwing an exception :

if(!month.HasValue) { throw ArgumentException("month"); }

You can allow this to bubble up to the appropriate location and handle it accordingly within your application (i.e. notify the user, log the problem, etc.)

Upvotes: 3

Matthias Burger
Matthias Burger

Reputation: 5946

int? is a Nullable-type, that means, that your int also could be null.

You get the value by

int? month = m_instr.InstrumentDetails.ExpirationDate.Month;
int month = month.Value;

you could check, if month is not null by

bool monthIsNotNull = month.HasValue;

because you could get an exception when trying to initialize the DateTime-variable when month is null - or at least, when trying to get int via month.Value and month is null

the DateTime-constructor wants DateTime(int year, int month, int day) - he doesn't want to get e.g. a month that's null

Upvotes: 1

CDove
CDove

Reputation: 1950

Assuming that the ExpirationDate.<properties> are your int? s, if the concern is whether or not there is an ExpirationDate, shouldn't you see if that is null instead? Then you could just use regular ints as properties of an ExpirationDate object.:

    private void m_getInstrDetails(Instrument instr)
    {
        m_ContractName = instr.Name.ToString();
        m_type = instr.Product.Type.ToString();
        m_prod = instr.Product.ToString();
        m_SmallestTickIncrtement = instr.InstrumentDetails.SmallestTickIncrement;

        if(InstrumentDetails.ExpirationDate != null)
        {
          //if you change the .Month, .Day, and .Year to int, and test against
          //ExpirationDate, this will work fine                        
          //month calc
          int month = m_instr.InstrumentDetails.ExpirationDate.Month; 
          int day = m_instr.InstrumentDetails.ExpirationDate.Day;
          int year = m_instr.InstrumentDetails.ExpirationDate.Year;
           m_expDate1 = new DateTime(year, month, day);
         }
    }

Upvotes: 0

Related Questions