Click here to Skip to main content
15,895,423 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
Ok guys I'm trying to learn how to create classes and use the get/set. I have played with this code for hours and have gotten myself so confused,, I don't know which way is up. Can someone point me in the right direction to make this code work? This is my updated code. It will let me enter both classes and number of assignments but the program ends there....anyone know why?

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmithMichaelFixedDebugNine01
{
    class Assignments
    {

        static void Main(string[] args)
        {
            HomeworkAssignment course1 = new HomeworkAssignment();
            HomeworkAssignment course2 = new HomeworkAssignment();
            string entry;


            // Get info for first class
            Console.Write("What class do you have homework for? ");
            entry = Console.ReadLine();
            course1.entry();
            Console.Write("How many exercises must you complete? ");
            entry = Console.ReadLine();
            course1.entry();


            // Get info for another class
            Console.Write("What class do you have homework for? ");
            entry = Console.ReadLine();
            course2.entry();
            Console.Write("How many exercises must you complete? ");
            entry = Console.ReadLine();
            course2.entry();

        }
        class HomeworkAssignment
        {
            
          // 10 minutes to complete each exercise
           internal void entry()
            {

            }
        }
        
        const int TIME_PER_EXERCISE = 10;
        private int numberOfExercises = 0;
        public int NumberOfExcercises
        {
            get
            {
                return numberOfExercises;
            }
            set
            {
                numberOfExercises = value;

            }
        }
        private int timeToComplete = 0;
        public int TimeToComplete
        {
            get
            {
                return timeToComplete;
            }
            set
            {
                timeToComplete = numberOfExercises * TIME_PER_EXERCISE;

            }


        }

    }
}
Posted
Updated 22-Mar-15 2:39am
v4

There are two forms of Property in C#: automatic and variable backed.
An automatic property leaves the system to handle the backing variable:
SQL
public int MyProperty { get; set; }

And the only way you can get access to the value is directly via the property name.

A variable backed property is different:
C#
private int _MyProperty = 666;
public int MyProperty
   {
   get { return _MyProperty; }
   set { _MyProperty = value; }
   }
Externally it is identical to an automatic property, but inside the class you can access the variable that holds the data directly:
C#
...
int x = _MyProperty;
...

What you are trying to do is combine the two, and that means every time you try and use the property, it ends up in an infinite loop trying to call the Getter or Setter from inside the Getter or Setter - and eventually, you app runs out of stack and crashes.

So try this:
C#
private int _TimeToComplete = 0;
public int TimeToComplete
    {
        get {return _TimeToComplete; }
        set { _TimeToComplete = NumberOfExercises * TIME_PER_EXERCISE;}
    }
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 20-Mar-15 12:12pm    
+5 for notifying him about the StackOverFlow exception. :)
When using get and set accessor. You need to use an extra variable. Your example will most likely create a StackOverflowException

Try this example:

C#
private int m_myInt = 0;
public MyInt
{
  get { return m_myInt; }
  set { m_myInt = value; }
}


The location you assign to and read from in the property cannot be the property itself.
 
Share this answer
 
Comments
Member 11403574 20-Mar-15 12:15pm    
Yes I do get a StackOverflowException, and your solution seems to have worked. The problem now is "timeToComplete = numberOfExercises * TIME_PER_EXERCISE;" The error says Operator '*' cannot be applied to operands of type int and object. I'm not sure what this means or how to fix it.
[no name] 20-Mar-15 12:20pm    
You should update your question with your reworked code :)
Sergey Alexandrovich Kryukov 20-Mar-15 12:31pm    
What could be not apparent here? Look what you are doing.
You call the getter of the property TimeToComplete.
In this getter, you return the same very TimeToComplete. It will call the same getter, ad infinitum.
All you need is TimeSpan TimeToComplete { get; set; }
—SA
Sergey Alexandrovich Kryukov 20-Mar-15 12:28pm    
Sorry, it won't compile. You forgot property type. Also, you did not mention that getter/setter does not do anything worth writing. This would be enough
public int MyProperty { get; set; }
internal string MyStringProperty { get; set; }
internal string MyOtherProperty { get; private set; }
—SA
Member 11403574 20-Mar-15 12:29pm    
I just reworked the code and updated. The problem now is at the bottom, says I can't use the operand * for an int and object, I thought they were both int's
Added after the inquirer updated the code after Solutions 1+2:

Note you're still using the property name in the property-getter and -setter (bold+underlined):
C#
private int timeToComplete = 0;
public int TimeToComplete
{
    get
    {
        return TimeToComplete;
    }
    set
    {
        TimeToComplete = numberOfExercises * TIME_PER_EXERCISE;
    }
}


It should be like this:
C#
private int timeToComplete = 0;
public int TimeToComplete
{
    get
    {
        return timeToComplete;
    }
    set
    {
        timeToComplete = numberOfExercises * TIME_PER_EXERCISE;
    }
}


The explanation of your new error "Operator '*' cannot be applied to operands of type int and object." regarding TimeToComplete = numberOfExercises * TIME_PER_EXERCISE;:

You have two TIME_PER_EXERCISE-constants defined. One is an int - but that's not the one that's being used by the compiler here ;)

Edit:
Further notes: You're doing nothing with the string entry that you're reading from the console, and your method HomeworkAssignment.entry() does nothing. I assume there should be some value being passed..
 
Share this answer
 
v2
Comments
Member 11403574 20-Mar-15 14:17pm    
I think I have messed everything up again. This is truly driving me mad. Again I have updated my code if anyone cares to have a look.
[no name] 20-Mar-15 14:26pm    
It doesn't compile, right?
[no name] 20-Mar-15 14:28pm    
If you don't have to have it finished by tomorrow I'd suggest take a break :-)
Member 11403574 20-Mar-15 14:31pm    
I compiles I guess, It just won't give the results. It let's me enter the 2 classes and the number of assignments but then it's just "press any key to continue" No results ....
[no name] 20-Mar-15 14:32pm    
Yeah, I noticed that - please take a look at the last bit of my solution above.
I annotated some of your code with comments, please have a look:

C#
static void Main(string[] args)
{
    HomeworkAssignment course1 = new HomeworkAssignment();
    HomeworkAssignment course2 = new HomeworkAssignment();
    string entry; 

    Console.Write("What class do you have homework for? ");

    // This line reads a user input from the Console and stores
    // that input into the variable "entry":
    entry = Console.ReadLine();

    // This line calls the method "entry()" of "course1".
    // See comment at method "entry()" below.
    course1.entry();

    Console.Write("How many exercises must you complete? ");

    // At this point "entry" still contains whatever was stored
    // into it previously. And we didn't do anything with it
    // in the meantime. Meaning that previous value will be
    // overwritten by the following line and completely lost.
    entry = Console.ReadLine();

    course1.entry();

    // Get info for another class
    // (snipped)
}

class HomeworkAssignment
{
    internal void entry()
    {
         // this method does absolutely nothing

         // i would recommend to rename it, because it's
         // confusing to have both a variable named entry
         // and a method named entry.
    }
}


To sum it up: You read user input from the console but you don't do anything with it. You need to store it from "entry" into some other variable which then will be used in the calculation of the total time after all user input has been read.

I would recommend you to study some C# samples/tutorials for beginners - just google for it, there are tons :)
 
Share this answer
 
Comments
Member 11403574 22-Mar-15 15:14pm    
Thanks so much for you input. I will do as you suggested. Thanks again.
[no name] 22-Mar-15 15:17pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900