Replace enum with struct inside a class

Aritra B

I know this is an absolute sitter, but somehow I'm not getting the hang of it. Lets say, I have a class Person which is as follows -

    public class Person
    {
        //Constructor...
        public Person( string name, Gender gender)
        {
            this.Name = name;
            this.Gender = gender;
        }

        //Member variables...


        private string _Name;
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }


        private Gender _Gender;
        public Gender Gender
        {
            get
            {
                return _Gender;
            }
            set
            {
                _Gender = value;
            }
        }
    }

    public enum Gender
    {
        Male = 0,
        Female = 1
    }

I do not want the Gender to be an enum anymore, I want this to be replaced with a struct and in the same way as above want to pass it as an argument to the constructor of the Person class such that when I create a new object of person class I am able to assign the gender as well to that person. How do I do that?

Thank you.

nima

I still don't get why you have to do this, but if you should you can do something like this:

    class Person
    {
        public string Name { get; private set; }
        public Gender Gender { get; private set; }

        public Person(string name, Gender gender)
        {
            Name = name;
            Gender = gender;
        }
    }

    struct Gender
    {
        private int _type;

        public bool IsMale
        {
            get { return _type == 0; }
            set { _type = value ? 0 : 1; }
        }

        public bool IsFemale
        {
            get { return _type == 1; }
            set { _type = value ? 1 : 0; }
        }
    }  

And to use it:

        var p1 = new Person("John", new Gender {IsMale = true});
        var p2 = new Person("Jane", new Gender {IsFemale = true});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamic initialization of enum class inside template struct

From Dev

Accessing a struct inside an enum

From Dev

Enum Inside Class

From Dev

Class inside a struct

From Dev

A string inside of a struct inside of a class

From Dev

What is the difference between enum struct and enum class?

From Dev

access enum class properties which defined in struct inside a function in c++

From Dev

class object array inside struct

From Dev

Updating a Struct property inside a Class

From Dev

Access a struct variable inside a class

From Dev

class object array inside struct

From Dev

Public struct inside a class and scopes

From Dev

How to get struct that is inside the class?

From Dev

Struct , class or enum for service object with static methods?

From Dev

c++: enum inside of a class using "enum class"

From Dev

Replace enum by class inTemplates c++

From Java

Enum inside class (TypeScript definition file)

From Dev

Private enum location inside a class in Java

From Dev

c++ enum class inside std::multimap

From Dev

(JAVA Enums) - Anonymous class inside enum constant

From Dev

c++ enum class inside std::multimap

From Dev

how to create a enum property inside a class with typescript?

From Dev

clojure access enum defined inside a java class

From Dev

How to use enum (which is defined inside a struct) as key of dictionary?

From Dev

Struct inside template class is not recognized by Visual Studio

From Dev

Defining a struct::matrix object inside a class method

From Dev

expose public struct inside a class for boost::python

From Dev

Accessing a struct inside of a private class C++

From Dev

c++ dynamic struct inside a class

Related Related

HotTag

Archive