Reputation: 5
I am working only with integers and want to create a new instance of Fraction. I receive the error that I "Cannot implicitly convert type. 'Fractions.Fraction' to 'int'". The problem has to do with creating the new instance in this specific method. I need to return all three values from the method back to the class.
I have stepped through the code with the debugger and verified that the values are getting changed correctly. I am just unable to get them to return correctly. The error is on the last line of the reduce method. Still somewhat new to C# and would appreciate any suggestions.
public static int Reduce(int wholeNum, int numer, int denomi)
{
int reduceWhole = (int)wholeNum;
while(numer > denomi)
{
numer -= denomi;
reduceWhole += 1;
}
while (denomi % numer == 0)
{
int factor = (int)denomi/numer;
numer = 1;
denomi = factor;
}
return new Fraction(reduceWhole, numer, denomi);
}
Upvotes: 0
Views: 81
Reputation: 13360
Your method Reduce
claims to return a value of type int
, but your return line is returning a type of Fraction
. You need to change the first line of your code from int
to Fraction
:
public static Fraction Reduce(int wholeNum, int numer, int denomi)
{
int reduceWhole = (int)wholeNum;
while(numer > denomi)
{
numer -= denomi;
reduceWhole += 1;
}
while (denomi % numer == 0)
{
int factor = (int)denomi/numer;
numer = 1;
denomi = factor;
}
return new Fraction(reduceWhole, numer, denomi);
}
Upvotes: 1
Reputation: 460340
Then simply change the signature of the method to return Fraction
instead of int
:
public static Fraction Reduce(int wholeNum, int numer, int denomi)
{
// ....
return new Fraction(reduceWhole, numer, denomi);
}
Upvotes: 3