Tony Slepian
Tony Slepian

Reputation: 53

How do I compare Enum values in class to info in main?

This is the class I have:

class student
{
    public string Name;
    public int Age;

    public enum Gender
    {
        male,
        female,
        other
    }

    public void Write()
    {
        Console.WriteLine("Name: {0}, Age: {1}, ", Name, Age);   
    }
}

And this is the main program:

class Program
{
    static void Main(string[] args)
    {
        student student1 = new student();
        student1.Name = "Dan";
        student1.Age = 15;
        student1.Write();
    }
}

When I run the program, the name and age variables from the main program are copied into the function Write in the class. I'm trying to do the same with the enum values - I want to write a gender variable in the main program, and add something to the fuction that will also write it, but I dont know how to do it with enum values.

If anyone can help I'd be happy to hear your suggetions.

Upvotes: 0

Views: 271

Answers (1)

Babbillumpa
Babbillumpa

Reputation: 1864

Add a Gender Type to your class:

public enum Genders { male, female, other }

class student
{
    public string Name;
    public int Age;
    public Genders Gender;

    public void Write()
    {
        Console.WriteLine("Name: {0}, Age: {1}, Gender: {2}", Name, Age, Gender.ToString());
    }
}

 class Program
{
    static void Main(string[] args)
    {
        student student1 = new student();
        student1.Name = "Dan";
        student1.Age = 15;
        student1.Gender = Genders.male;
        student1.Write();
    }
}

Upvotes: 3

Related Questions