This is a homework, and we don't learn the structs in C# (and I am learn now). So I go to the msdn, but I don't find what my program's problem... Yes, and I can't translate fully assigned to my native languange, and I don't understadn what the compiler thinks.
UML of my homework: (we must use it) Interface: IComparable
- +Numerator{get;}:long
- +Denominator{get;}:long
- +Rational(numerator:long, denominator:long)
- +GCD(a:long, b:long):long
- +Equals(r:Rationaol):bool
- ...
And we mustn't implement another methods or params.
I write this homework in Java with classes and that work correctly.
I have a big problem, I don't understand what mean the following erros:
Error 2 Backing field for automatically implemented property 'Rational.Rational.Denominator' must be fully assigned before control is returned to the caller. Consider calling the default constructor from a constructor initializer.
Error 3 Backing field for automatically implemented property 'Rational.Rational.Numerator' must be fully assigned before control is returned to the caller. Consider calling the default constructor from a constructor initializer.
Error 4 The 'this' object cannot be used before all of its fields are assigned to
my code: namespace Rational
{
// (Rational is underline) here the 2 and 3 error
public struct Rational : IComparable<Rational>
{
public long Numerator { get; set; }
public long Denominator { get; set; }
public Rational(long num, long den)
{
// and here (GCD is underline) the 4. error
long simple = GCD(num, den);
this.Numerator = num/simple;
this.Denominator = den/simple;
}
public long GCD(long a, long b)
{
long c = Math.Abs(a);
long d = Math.Abs(b);
if (d == 0) return c;
return GCD(d, c % d);
}
public override string ToString()
{
if (Denominator==1)
{
return Numerator + "";
}
return Numerator+"/"+Denominator;
}
public override bool Equals(object obj)
{
bool result = false;
if (obj is Rational)
{
Rational r = (Rational)obj;
result = this.Equals(r);
}
return result;
}
public bool Equals(Rational r)
{
if ((object)r ==null)
{
return false;
}
return this.Denominator == r.Denominator && this.Numerator == r.Numerator;
}
public int CompareTo(Rational other)
{
...