Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Friends,
I am not able find out the problem in this code.
My output should be - when i pass the value of Mark as 1 it should return 0 and when i pass as 20 , it should return 20 (via the property 'Mark'). But currently its returning only 0,0 in all cases.
Please help.
C#
class Program
   {
       static void Main(string[] args)
       {
           Encapsulation encapsulate = new Encapsulation();
           encapsulate.Mark = 1;
           encapsulate.Display();
           encapsulate.Mark = 20;
           encapsulate.Display();
           Console.ReadKey();
       }
   }

   public class Encapsulation
   {
       private int _mark;
       public int Mark
       {
           get { return _mark; }
           set
           {
               if (_mark < 5)
                   _mark = 0;
               else
                   _mark = value;
           }
       }

       public void Display()
       {
           Console.WriteLine("Mark: " + Mark);
       }
   }
Posted
Comments
Debug into the "set". You will identify the problem.

1 solution

I suspect that if (_mark < 5) should be if (value < 5)
 
Share this answer
 
Comments
phil.o 11-Sep-15 23:29pm    
It has to :) 5'd

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