CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2012
    Posts
    1

    Newb question. I am just begining to use C#

    Hello guys,

    I am just starting to use C# for school. I like the program so far but I have lots to learn. I am having problems with properties. This is an assignment for school but I don't know how to do it.

    I need to know how to code the properties.
    This is the code so far:
    namespace Assignment2
    {
    public class Student
    {
    public Student(string n)
    {
    Name = n;
    Record = new Transcript();
    }
    // To do: Property implementation
    public string Name;
    public Transcript Record;
    public double GPA
    {
    get
    {
    return Record.GPA;
    }
    }
    public int WealthPoints = 10000;
    public int PridePoints = 100;
    }
    }

    then I'm given a table to add some properties?
    this is what the table looks like:
    Student
    -m_record : Transcript
    -m_name : string
    -m_wealth : int = 1000
    -m_pride : int = 100
    + Student(string);
    +Name : string
    +WealthPoints : int
    +PridePoints : int
    +GPA : double
    +Record: Transcript

    Any advice would be really helpful. Thanks.


    I am using Visual C# 2010 Express

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Newb question. I am just begining to use C#

    Sometimes it makes sense to encapsulate data by making the main state of an object hidden, but accessible through properties. For example, suppose I wanted to represent a person with a name (string) and age (int). I might write:

    Code:
    public class Person
    {
        //Declare private variables; accessible only within this class
        private string name;
        private int age;
    
        //Constructor
        public Person(string newName, int newAge)
        {
            this.name = newName;
            this.age = newAge;
        }
    
        //Properties
    
        //Other classes can both get and set a Person's name
        public string Name
        {
            //Defines a getter method; always named get
            get { return this.name; }
    
            /*Define a setter method; always named set
             *The setter method will accept an argument (in this case a string)
             * that will be stored in a reserved variable (keyword) called value
            set { this.name = value; }
        }
    
        //But let's only let them get (NOT modify) age
        public int Age
        {
            //Define the getter
            get { return this.name; }
    
            //But omit the setter
        }
    }
    Make sense?
    Last edited by BioPhysEngr; February 15th, 2012 at 09:59 PM. Reason: wrong bbcode
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured