using Enum data type in constructor?

Hi,
can someone make a simple example of using enum data type.
i know how to define them

public enum Gender { Male, Female }

but how can i pass it to constructor and use it there?
Raul77Asked:
Who is Participating?
 
mac-willConnect With a Mentor Commented:
By creating an ENUM you are essentially making a new type (like an integer) so you could have something like this:

Human  mH = new Human  (Human.Gender.Male, 33);

than you have an instance of a Human.  The Human class might look like this:
public class Human
{
 
     public enum Gender { Male, Female }
 
 
     private Gender mGender;
     private int mAge;
 
     public Human(Gender theGender, int theAge)
     {
            mGender = theGender;
            mAge = theAge;
     }
 
}

Open in new window

0
 
mac-willCommented:

public class MyClass
{
 
     public enum Gender { Male, Female }
 
     public MyClass(Gender theGender)
     {
            // do something with theGender variable
      }
 
}

Open in new window

0
 
Raul77Author Commented:
mmm ok lets say i am making a human object,
the user specifies Male as gender, how to i pass it to my object

Gender = theGender;  ???
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Like this you mean?

    MyClass mc = new MyClass(MyClass.Gender.Male);
0
 
Raul77Author Commented:
i have a form that user signs up, the specify their gender
once they click submit
i want to create a user object

how can i pass to constructor what the user selected (male or female)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You already have an example of how to create a Human instance using the enum...are you asking how to read what was selected on the Form?

If so, we would need to see how you are calling the sign-up form and what you allowing the user to specify gender with...
0
 
Raul77Author Commented:
perfect, Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.