john555
john555

Reputation: 71

Property Set method not working properly - any fix?

Executing this code gives me two type of errors and I'm not sure why it isn't working.

Error1: When I execute the code, I don't know why it says that name is null in line14

Error2: When I give the string student name a value manually, the grade1 and grade2 are still stored, even if I give it a value outside the if condition.

Any idea why this happens?

class Student
{
    private string name;
    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            if (name.Length <= 15)
            {
                name = value;
            }
            else
            {
                Console.WriteLine("Name length is bigger than 15, please re-run the program");
            }
        }
    }

    public int grade1;
    public int Grade1
    {
        get
        {
            return grade1;
        }
        set
        {
            if (grade1 >= 0 && grade1 <= 100)
            {
                grade1 = value;
            }
            else
            {
                Console.WriteLine("The grade entered isn't between 0 and 100, please re-run the program");
            }
        }
    }

    public int grade2;
    public int Grade2
    {
        get
        {
            return grade2;
        }
        set
        {
            if (grade2 >= 0 && grade2 <= 100)
            {
                grade2 = value;
            }
            else
            {
                Console.WriteLine("The grade entered isn't between 0 and 100, please re-run the program");
            }
        }
    }

    public double Average
    {
        get
        {
            return (grade1 + grade2) / 2;
        }
    }

    static void Main(string[] args)
    {
        //student1 input//
        Student student1 = new Student();

        string values1 = Console.ReadLine();
        student1.Name = values1.Split(' ')[0];
        student1.Grade1 = Convert.ToInt32(values1.Split(' ')[1]);
        student1.Grade2 = Convert.ToInt32(values1.Split(' ')[2]);
        Console.Write($"The average of student 1 is {student1.Average}");
    }

}

Upvotes: 1

Views: 533

Answers (1)

Marco Salerno
Marco Salerno

Reputation: 5203

This is the approach you want to take:

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Student student1 = new Student();

            string[] values = Console.ReadLine().Split(' ');

            student1.Name = values[0];

            student1.Grade1 = Convert.ToInt32(values[1]);

            student1.Grade2 = Convert.ToInt32(values[2]);

            Console.Write($"The average of student 1 is {student1.Average}");
        }
    }

    public class Student
    {
        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }

            set
            {
                if (value.Length <= 15)
                {
                    _name = value;
                }
                else
                {
                    Console.WriteLine("Name length is bigger than 15, please re-run the program");
                }
            }
        }

        public int _grade1;
        public int Grade1
        {
            get
            {
                return _grade1;
            }
            set
            {
                if (value >= 0 && value <= 100)
                {
                    _grade1 = value;
                }
                else
                {
                    Console.WriteLine("The grade entered isn't between 0 and 100, please re-run the program");
                }
            }
        }

        private int _grade2;
        public int Grade2
        {
            get
            {
                return _grade2;
            }
            set
            {
                if (value >= 0 && value <= 100)
                {
                    _grade2 = value;
                }
                else
                {
                    Console.WriteLine("The grade entered isn't between 0 and 100, please re-run the program");
                }
            }
        }

        public double Average => (Grade1 + Grade2) / 2;
    }
}

I made many changes:

  1. Your program class must be separated to your "model" in this case, Student.
  2. Executing split everytime is useless, you just had to do it one time and reuse the results
  3. Checking the private variable value in your set is useless, you wanted to check the inserted value which is inside of the set statement: value
  4. In your average function by not using parenthesis you are executing Grade2 / 2 and then Grade1 addition, in my answer you will execute it correctly

Upvotes: 3

Related Questions