class property

gmcconville

New member
Joined
Jul 21, 2015
Messages
2
Programming Experience
10+
Good afternoon.

Can someone please let me know if you can treat a class as a property?

Like this...
public class person
{
    private string name;
    public string Sex;

    public person()
    {
        get { return person; }
        set { person = value; }
    }
}


.... Other code
person Sarah = new person()
Sarah.Sex = "Female"
string customername = Sarah


I realize I can just make the name value public, but this purely more curiosity, if you access a variable in a class by only referencing the class
 
Last edited by a moderator:
The answer is "no". If what you're expecting to do is get the 'name' value from the 'person' object simply by providing the 'person' object itself then how would you expect that to work? How would the system know whether you wanted the object or its field value?

If the field value you want is a string then you could override the ToString method of the class and then call that. The point of the ToString method is to provide a string representation of the object. If you consider that name to represent the object then you can have the ToString method return it. You could also have ToString return a combination of field values, like Point.ToString or Size.ToString do.
 
Back
Top Bottom