Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
So I'm self-teaching myself C# (with the help of many books and websites), and in trying to understand properties I went to the MSDN website. I copied the following code almost verbatim (just changing a couple object names), and I can't understand why it does what it does. Basically, from my understanding the "set" part of a property gets fired every time you write something like ob.MyProp = variable. So then why in this code does the convert.Hours = userInput always return a value of 24? Shouldn't it progressively get higher and higher as it's multiplied by the seconds over and over?

Thanks for any help!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main()
        {
            Time convert = new Time();


            Console.WriteLine("Enter a number greater than 0: ");
            double userInput = double.Parse(Console.ReadLine());
            convert.Hours = userInput;
            Console.WriteLine("There are {0} seconds in {1} hours.", convert.Hours, (convert.Hours = userInput));
            Console.WriteLine(convert.Hours = userInput);
            Console.WriteLine(convert.Hours = userInput);
            Console.WriteLine(convert.Hours = userInput);
            Console.WriteLine(convert.Hours = userInput);
        }

    }

    class Time
    {
        private double seconds;
        public double Hours
        {
            get
            {
                return seconds;
            }
            set
            {
                seconds = value * 3600;
            }
        }
    }
}
Posted
Comments
Dr.Walt Fair, PE 15-Jul-11 18:50pm    
Your code is doing just what it should be doing. Why did you expect something different?
Philippe Mori 15-Jul-11 19:53pm    
This is bad code as you uses and modify convert.Hours in the same statement. It is not obvious what should be the result. In C++, it would be undefined. I don't in C#.
Philippe Mori 15-Jul-11 19:55pm    
This code does not make sence as every time you set the property, you multiply it by 3600 by when you retreive it, you get the multiplied value. If you set 1 hour, you should not get 3600 hours. Very bad.

1 solution

NO. In this case you are assigning the userInput to the property and not reading it (which in this case is as passing to the WriteLine method the userInput variable).

try this

MIDL
convert.Hours = userInput;
Console.WriteLine(convert.Hours);


First assign a value (call the setter) and then by passing the property as a parameter, method WriteLine will call the getter.

What are you trying to achieve precisely?

P.S.
Another thing, it will never increment, you are always assagning the same thin to seconds, in order to make it "grow" you need to do something like this seconds += value * 3600; or however implement the proper algorithm that considers the seconds value.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 15-Jul-11 20:06pm    
This is a correct answer, my 5. However, I feel OP did not get it, see (deleted) reply posted as solution. This is the question of the class "garbage in, garbage out".
--SA

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