-3

I'm currently iterating through a list of items(hotels) and I need to find out the price per night if the content editor inputs more than 1 night's price.

if (!string.IsNullOrEmpty(resource))
{
    var results = JsonConvert.DeserializeObject(resource).ToString();
    if (!string.IsNullOrEmpty(results))
    {
        var hotels = JsonConvert.DeserializeObject<ContainerHotelViewModel>(results).Hotels;

        if (daysDuration > 1)
        {
            foreach (var hotel in hotels)
            {
                string convertInt = hotel.BaseRate;
                int nightRate;
                int.TryParse(hotel.BaseRate, out nightRate);
                convertInt = nightRate / daysDuration;                    
            }
        }

        return SortHotels(hotelIds, hotels);
    }
}

So my understanding is that I've converted hotel.BaseRate from a string to an int. So it makes sense for me to divide the hotel.BaseRate by the daysDuration to get the price per night as they are both ints now. Please advise on what I'm doing wrong as my final line of code in the foreach loop is giving me the error message:

Cannot implicitly convert type int to string.

BWA
  • 5,672
  • 7
  • 34
  • 45
Jim41Mavs
  • 482
  • 4
  • 21
  • Please for future question provide [MCVE]. I.e. in this case it would be `string convertInt = 4 / 2;` which could have helped you to find issue in the first place. – Alexei Levenkov Oct 21 '16 at 14:40

6 Answers6

4

You defined convertInt as string earlier in the line:

string convertInt = hotel.BaseRate;
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
3
string convertInt = hotel.BaseRate;
int nightRate;
int.TryParse(hotel.BaseRate, out nightRate);
convertInt = nightRate / daysDuration;

You first declare convertInt as a string and then try to set it to response from your equation.

Remove first line and set last to this:

int convertInt = nightRate / daysDuration;
J.R.
  • 167
  • 5
2

You cannot convert an int to a string (convertInt is the string in this instance).

You can however, do this:

convertInt = (nightRate / daysDuration).ToString();

Also, as a side note: If you're working with money you should not use an Int, as if you were to devide £15 with 3 nights, the answer would be £5.33 - but int's cannot hold decimal places.

Mark Phillips
  • 273
  • 1
  • 8
  • thanks for your help and I will take your advice regarding not using int for money! – Jim41Mavs Oct 21 '16 at 14:35
  • Apologies @Jim41Mavs, It seems i totally forgot to put what type of variable you should use. Instead of Int, use a Double :) Also, do some research on the 'var' command for declaring variables. – Mark Phillips Oct 21 '16 at 14:37
  • 1
    Just as a note, decimal is prefered when handling currency :) http://stackoverflow.com/a/1165788/6251539 – J.R. Oct 21 '16 at 14:58
1

Please change your code .

Old code :  convertInt = nightRate / daysDuration;  

New code :  convertInt = (nightRate / daysDuration).ToString();  

Please update with your old code line with new code line .

Thanks .

Ronak Patel
  • 630
  • 4
  • 15
1

Your poblem is here:

foreach (var hotel in hotels)
{
    string convertInt = hotel.BaseRate;
    int nightRate;
    int.TryParse(hotel.BaseRate, out nightRate);
    convertInt = nightRate / daysDuration;  
}

convertInt is string and you try to save into it int value form nightRate / daysDuration. You need to use (nightRate / daysDuration).ToString()

So your line:

convertInt = nightRate / daysDuration;

Should be:

convertInt = (nightRate / daysDuration).ToString();
BWA
  • 5,672
  • 7
  • 34
  • 45
0

Your error is of mixing up types. The program expects a string where there is an int so you will have to call .ToString() on the int. Better yet is to refactor your code so that all variables needed are defined with proper types before being called for calculations. By the way, the recommended type for currency is Decimal. The problem with int is that you cannot represent its decimal units such as the cent, and there are rounding problems with using double.

Viet Trang
  • 51
  • 5