Click here to Skip to main content
15,914,413 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to check " if " condition in a property in c#.net?
Posted
Updated 22-Nov-11 19:53pm
v2
Comments
Sergey Alexandrovich Kryukov 23-Nov-11 2:08am    
"if" is not condition, it is a statement, a condition comes in brackets.
Not clear what's the problem.
--SA

You can do this -

public int myProp
 {
     get { if (condition) { return a; } else { return b;} }
     set
     {
         a = value;
     }
 }
 
Share this answer
 
Comments
Mehdi Gholam 23-Nov-11 2:03am    
Simple! 5'ed
Abhinav S 23-Nov-11 2:59am    
Thank you Mehdi.
Use following code that contains if conditions in properties using C#

C#
private double seconds;

public double Hours
{
    get
    {
        if (seconds == 3600)
        {
            return 100;
        }
        else
        {
            return seconds / 3600;
        }
    }
    set
    {
        if (seconds == 3600)
        {
            seconds = 100;
        }
        else
        {
            seconds = value * 3600;
        }


    }
}
 
Share this answer
 

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