?
Solved

using Enum data type in constructor?

Posted on 2009-02-18
7
Medium Priority
?
1,756 Views
Last Modified: 2012-05-06
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?
0
Comment
Question by:Raul77
  • 3
  • 2
  • 2
7 Comments
 
LVL 8

Expert Comment

by:mac-will
ID: 23673077

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

Open in new window

0
 

Author Comment

by:Raul77
ID: 23673124
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23674074
Like this you mean?

    MyClass mc = new MyClass(MyClass.Gender.Male);
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Raul77
ID: 23674101
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
 
LVL 8

Accepted Solution

by:
mac-will earned 2000 total points
ID: 23674240
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23674646
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
 

Author Closing Comment

by:Raul77
ID: 31548382
perfect, Thanks
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Loops Section Overview
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question