Inconsistent accessibility: property type 'Exercises.Register.Gender' is less accessible than proper
I honestly have no clue how it's having issues with accessibility when both classes are within the same namespace and Dog.cs has no issue interacting with Gender enum in the exact same way.
Animal.cs
Gender.cs
namespace Exercises.Register
{
public abstract class Animal
{
public int ID { get; set; }
public string Name { get; set; }
public string Breed { get; set; }
public DateTime BirthDate { get; set; }
public Gender Gender { get; set; }
public DateTime LastVaccinationDate { get; set; }
public int Age
{
get
{
DateTime today = DateTime.Today;
int age = today.Year - this.BirthDate.Year;
if (this.BirthDate.Date > today.AddYears(-age))
{
age--;
}
return age;
}
}
public abstract bool RequiresVaccination { get; }
public Animal(int id, string name, string breed, DateTime birthDate, Gender gender)
{
this.ID = id;
this.Name = name;
this.Breed = breed;
this.BirthDate = birthDate;
this.Gender = gender;
}
public override bool Equals(object other)
{
return this.ID == ((Animal)other).ID;
}
public override int GetHashCode()
{
return this.ID.GetHashCode();
}
public int CompareTo(Animal other)
{
int result = this.Breed.CompareTo(other.Breed);
if (result == 0)
{
return this.Gender.CompareTo(other.Gender);
}
return result;
}
}
}
namespace Exercises.Register
{
public abstract class Animal
{
public int ID { get; set; }
public string Name { get; set; }
public string Breed { get; set; }
public DateTime BirthDate { get; set; }
public Gender Gender { get; set; }
public DateTime LastVaccinationDate { get; set; }
public int Age
{
get
{
DateTime today = DateTime.Today;
int age = today.Year - this.BirthDate.Year;
if (this.BirthDate.Date > today.AddYears(-age))
{
age--;
}
return age;
}
}
public abstract bool RequiresVaccination { get; }
public Animal(int id, string name, string breed, DateTime birthDate, Gender gender)
{
this.ID = id;
this.Name = name;
this.Breed = breed;
this.BirthDate = birthDate;
this.Gender = gender;
}
public override bool Equals(object other)
{
return this.ID == ((Animal)other).ID;
}
public override int GetHashCode()
{
return this.ID.GetHashCode();
}
public int CompareTo(Animal other)
{
int result = this.Breed.CompareTo(other.Breed);
if (result == 0)
{
return this.Gender.CompareTo(other.Gender);
}
return result;
}
}
}
namespace Exercises.Register
{
enum Gender
{
Male = 1,
Female = 2,
}
}
namespace Exercises.Register
{
enum Gender
{
Male = 1,
Female = 2,
}
}
6 Replies
Dog.cs
Hmm just looked over it again and figured that class being abstract is causing that. How should I go around this? The task gave me this code
namespace Exercises.Register
{
class Dog
{
public int ID { get; set; }
public string Name { get; set; }
public string Breed { get; set; }
public DateTime BirthDate { get; set; }
public Gender Gender { get; set; }
private const int VaccinationDuration = 1;
public DateTime LastVaccinationDate { get; set; }
public Dog(int id, string name, string breed, DateTime birthDate, Gender gender)
{
this.ID = id;
this.Name = name;
this.Breed = breed;
this.BirthDate = birthDate;
this.Gender = gender;
}
public int Age
{
get
{
DateTime today = DateTime.Today;
int age = today.Year - this.BirthDate.Year;
if (this.BirthDate.Date > today.AddYears(-age))
{
age--;
}
return age;
}
}
public bool RequiresVaccination
{
get
{
if (LastVaccinationDate.Equals(DateTime.MinValue))
{
return true;
}
return LastVaccinationDate.AddYears(VaccinationDuration)
.CompareTo(DateTime.Now) < 0;
}
}
public override bool Equals(object other)
{
return this.ID == ((Dog)other).ID;
}
public override int GetHashCode()
{
return this.ID.GetHashCode();
}
public int CompareTo(Dog other)
{
int gender = this.Gender.CompareTo(other.Gender);
if (gender != 0)
{
return gender;
}
return this.Breed.CompareTo(other.Breed);
}
}
}
namespace Exercises.Register
{
class Dog
{
public int ID { get; set; }
public string Name { get; set; }
public string Breed { get; set; }
public DateTime BirthDate { get; set; }
public Gender Gender { get; set; }
private const int VaccinationDuration = 1;
public DateTime LastVaccinationDate { get; set; }
public Dog(int id, string name, string breed, DateTime birthDate, Gender gender)
{
this.ID = id;
this.Name = name;
this.Breed = breed;
this.BirthDate = birthDate;
this.Gender = gender;
}
public int Age
{
get
{
DateTime today = DateTime.Today;
int age = today.Year - this.BirthDate.Year;
if (this.BirthDate.Date > today.AddYears(-age))
{
age--;
}
return age;
}
}
public bool RequiresVaccination
{
get
{
if (LastVaccinationDate.Equals(DateTime.MinValue))
{
return true;
}
return LastVaccinationDate.AddYears(VaccinationDuration)
.CompareTo(DateTime.Now) < 0;
}
}
public override bool Equals(object other)
{
return this.ID == ((Dog)other).ID;
}
public override int GetHashCode()
{
return this.ID.GetHashCode();
}
public int CompareTo(Dog other)
{
int gender = this.Gender.CompareTo(other.Gender);
if (gender != 0)
{
return gender;
}
return this.Breed.CompareTo(other.Breed);
}
}
}
can you share the whole error?
abstract
is not the problem here
if you read the whole error it will give you a clueThose are the only errors that it shows
I made Enum public and it seems to work
But can someone explain briefly how that works?
yes, your abstract class is declared
public
so all the types of its externally accessible members must also be public
your Dog
class is not so it defaults to internal
accessibility, which is why it was working there