0

I have a class called Parrot that inherits from Bird class that inherits from Animal class. Class Animal has enum genderType. However, when I am trying to make a new object of Parrot and assign the enum value like this:

Animal.cs

internal class Animal
    {
        private int id;
        private String name;
        private int age;
        enum GenderType { Female, Male, Unknown }

        GenderType gender;
        public int Id { get => id; set => id = value; }
        public string Name { get => name; set => name = value; }
        public int Age { get => age; set => age = value; }

    }
}

Form1.cs

.
.
          pr = new parrot();
          pr.Name = name.Text;
          pr.Age = int.Parse(age.Text);
          ((parrot)pr).Color = extraInfo.Text;
          ((parrot)pr).FlyingSpeed = int.Parse(fSpeed.Text);
          pr.gender = GenderType.Female;
.
.

Bird.cs

 internal class Bird:Animal
    {
        private int flyingSpeed;

        public int FlyingSpeed { get => flyingSpeed; set => flyingSpeed = value; }
    }
}

parrot.cs

internal class parrot:Bird
    {
        private string color;

        public string Color { get => color; set => color = value; }
    }
}

I am getting these erors:

The name 'genderType' does not exist in the current context

'Animal.genderType' is a type but is used like a variable

'genderType': cannot reference a type through an expression; try 'Animal.genderType' instead

4
  • 2
    Can you show rest of the code? Or just parrot Commented Mar 13, 2022 at 19:23
  • 2
    Indeed - a minimal reproducible example will help a lot here. I would also suggest following .NET naming conventions, where both types and properties are PascalCased. Commented Mar 13, 2022 at 19:24
  • As declared now, the enum is private to the class Animal. Either ove it out of the class or make it public. Commented Mar 14, 2022 at 5:07
  • The field is also private, and is not exposed by any public property. Commented Mar 14, 2022 at 6:51

1 Answer 1

1

Inner types in classes, like:

class A {
    enum B { C = 0 }
}

Are not variables. If you want a variable bound to Animal and change it when needed you need a variable:

class Animal {
    //type
    enum GenderType { ... }

    //variable
    GenderType gender;
}

//code:
var animal = new Animal();
animal.gender = GenderType.Female;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.