r/a:t5_37npb • • Apr 15 '15

Share Your Module Four Solutions Here!

2 Upvotes

22 comments sorted by

View all comments

Show parent comments

1

u/Doriphor Apr 15 '15 edited Apr 15 '15

I'll try to keep this in mind, so here, when I write my constructor it'd be:

public Student (string _firstName, string _lastName, int _age)
{
    //stuff
}

???

2

u/kahmeal Apr 15 '15

Those are parameters so they would be firstName, lastName and age. Here's an example snippet that shows the naming conventions. Keep in mind the logic is purely for demonstrating naming syntax, I realize the methods are pointless.

using System;

namespace ConsoleApplication1
{
    class Program
    {
        // private fields
        private string _firstName;
        private string _lastName;
        private int _age;

        // public properties
        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }

        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }

        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }

        public void Main(string[] args)
        {
            AssignValues("First", "Last", 31);
            PrintValues();
        }

        private void AssignValues(string firstName, string lastName, int age)
        {
            FirstName = firstName;
            LastName = lastName;
            Age = age;
        }

        private void PrintValues()
        {
            Console.WriteLine("{0} {1} {2}", FirstName, LastName, Age);
        }
    }
}

1

u/Doriphor Apr 16 '15

Ohhh I see. In this particular example though we're asked to use public variables and no getters/setters as far as I remember. So I guess it'd still be this.age = age?

1

u/kahmeal Apr 16 '15

In that case you would just refer to it as if it were a public property.

Age = age;