J
JohnGoogle
Hi,
Newbie question...
After a recent article in VSJ I had a go at implementing a Fraction
class to aid my understanding of operator overloading. After a previous
message someone suggested that I implement it as a struct rather than a
class which I did and all worked OK.
The simplest declaration for the struct is:
public struct Fraction
{
private Int32 _numerator;
private Int32 _denominator;
public Int32 numerator
{
get { return _numerator; }
}
public Int32 denominator
{
get { return _denominator; }
}
public Fraction(Int32 numerator)
{
this._numerator = numerator;
this._denominator = 1;
}
public Fraction(Int32 numerator, Int32 denominator)
{
this._numerator = numerator;
this._denominator = denominator;
if (denominator < 1)
throw new ArgumentException("Fraction denominator value
'" + denominator.ToString() + "' is invalid.");
}
// All other declarations follow (for overloads of
+,-,*,/,++,--,>,<, etc).
}
As you can see, it is essential that the denominator must be 1 or more.
As it was originally a class, the constructors ensured that the
denominator was >= 1. Alos, the numerator and denominator values are
read only as they can only be modified by operators such as +,-,++,*,/
etc (not shown).
However, during my testing I realised that, as a struct, it is possible
to declare a struct which is initialised to 0 so the denominator
becomes illegal.
I've found that you cannot declare a parameterless construction such
as:
public struct Fraction
{
private Int32 _numerator;
private Int32 _denominator;
....
public Fraction()
{
this._numerator = 0;
this._denominator = 1;
}
....
}
This gives a compiler error: 'Structs cannot contain explicit
parameterless constructors'.
Also, you cannot initialise the fields of a struct such as:
public struct Fraction
{
private Int32 _numerator;
private Int32 _denominator = 1;
....
}
This gives a compilation error: '_denominator': cannot have instance
field initializers in structs'.
Is there any way to force a field of a struct to be initialised to a
non zero value?
If not, I'll go back to implementing it as a class.
TIA,
John.
Newbie question...
After a recent article in VSJ I had a go at implementing a Fraction
class to aid my understanding of operator overloading. After a previous
message someone suggested that I implement it as a struct rather than a
class which I did and all worked OK.
The simplest declaration for the struct is:
public struct Fraction
{
private Int32 _numerator;
private Int32 _denominator;
public Int32 numerator
{
get { return _numerator; }
}
public Int32 denominator
{
get { return _denominator; }
}
public Fraction(Int32 numerator)
{
this._numerator = numerator;
this._denominator = 1;
}
public Fraction(Int32 numerator, Int32 denominator)
{
this._numerator = numerator;
this._denominator = denominator;
if (denominator < 1)
throw new ArgumentException("Fraction denominator value
'" + denominator.ToString() + "' is invalid.");
}
// All other declarations follow (for overloads of
+,-,*,/,++,--,>,<, etc).
}
As you can see, it is essential that the denominator must be 1 or more.
As it was originally a class, the constructors ensured that the
denominator was >= 1. Alos, the numerator and denominator values are
read only as they can only be modified by operators such as +,-,++,*,/
etc (not shown).
However, during my testing I realised that, as a struct, it is possible
to declare a struct which is initialised to 0 so the denominator
becomes illegal.
I've found that you cannot declare a parameterless construction such
as:
public struct Fraction
{
private Int32 _numerator;
private Int32 _denominator;
....
public Fraction()
{
this._numerator = 0;
this._denominator = 1;
}
....
}
This gives a compiler error: 'Structs cannot contain explicit
parameterless constructors'.
Also, you cannot initialise the fields of a struct such as:
public struct Fraction
{
private Int32 _numerator;
private Int32 _denominator = 1;
....
}
This gives a compilation error: '_denominator': cannot have instance
field initializers in structs'.
Is there any way to force a field of a struct to be initialised to a
non zero value?
If not, I'll go back to implementing it as a class.
TIA,
John.