Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Everyone!

I Am Trying to understand the Property Method in C#. But During Setting the Value it Did not check the Condition and give me Wrong Answer .Please See the Code me Tell me Good Solution if Possible.

Code

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

namespace PracticeQuestion
{
    class ElementContainer
    {
        private double Length;
        private double Width;

        //Setter and Getter Property

        public double SetLength
        {
            get { return Length;}
            set
            {
                if (Length > 10)
                {
                    Console.Write("\n\nThere is some Bad Input.");
                }
                else
                {
                    Length = value;
                }
            }
        }
        public double SetWidth
        {
            get
            {
                 return Width;
            }
            set
            {
                if (Width > 10)
                {
                    Console.Write("\n\nThere is some Bad Input.");
                }
                else
                {
                    Width = value;
                }
            }
        }
        public ElementContainer ()
        {
            Console.Write("Please Enter The Following Data.");
        }
        public override string ToString ()
        {
            return string.Format("\n\nlength:{0}\n\nWidth:{1}\n\nTotal Area:{2}", Length, Width,Length*Width);
        }
    }
}



C#
class Program
   {
       static void Main ( string[] args )
       {
           ElementContainer Element = new ElementContainer();

//Error Is There

when i set The Value Then i want first it check the Setter Condition then set value According to required Condition.

           Element.SetLength = 10;
           Element.SetWidth = 20;
           Console.Write(Element);
           Console.ReadLine();
       }
   }
Posted
Comments
Sergey Alexandrovich Kryukov 5-Oct-14 2:28am    
Not clear. This code does not seem to be anything practically reasonable, but the problems is not clear.
You limit the size by some previously set size: when it's more then 10, property assignment is ignored, so there is no a possibility to reduce it back. "Wrong Answer" is not defined. "Wrong" depends on your goal, which you did not bother to share with us.
—SA

1 solution

If setter encounters invalid argument, your option is not to write to the console, since that won't provide any information to the caller. You should throw an exception in that case.
Something like this:
C#
public double SetWidth
        {
            get
            {
                 return Width;
            }
            set
            {
                if (value > 10)
                {
                    throw new ArgumentOutOfRangeException("There is some Bad Input.");
                }
                else
                {
                    Width = value;
                }
            }
        }


On the caller side you need try-catch blocks to intercept this.
An other note based on SA's catch: in the setter you need to validate the new value, not the stored one.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 5-Oct-14 2:33am    
Of course, but the whole idea looks so weird that this console thing seems to be a minor peculiarity. OP could have done this only for research purpose. Note that not value is compared, but Width. It's not "bad input"; the exception does not depend in "input", it depends on previous value.
—SA
Zoltán Zörgő 5-Oct-14 2:35am    
Right! I overlooked this.
Sergey Alexandrovich Kryukov 5-Oct-14 2:42am    
Sure, it would look much more reasonable, but OP did not bother to share with us the intention of the code. :-)
—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