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.
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.
Comments