user1005310
user1005310

Reputation: 877

Conditional logic in the class property

public class test
{
    public datetime date1 {get;set;}
    public datetime date2 {get;set;}
    public string status {get;set;}
}

Now the value of property status is calculated based on the values for date1 and date2

For example

if date1 > dataetime.today 
status ="active"
else
status = "inactive"

I think I need to write the logic in the set of the property status. How do I achieve this ?

Upvotes: 0

Views: 1343

Answers (3)

Sleepy Panda
Sleepy Panda

Reputation: 458

public string Status
{
    get
    {
        // your code
    }
    set
    {
        // your code
    }
}

You can read more about get and set accessors here.

Upvotes: 0

Orel Eraki
Orel Eraki

Reputation: 12196

If you want it to always be synchronized with date1 then you should make status a getter.

public string status
{
    get
    {
        return date1 > DateTime.Today ? "Active" : "Inactive";
    }
}

Note: I would strongly recomend you to follow C# Capitalization Conventions

Upvotes: 1

A.B.
A.B.

Reputation: 2470

public class test
{
    public datetime date1 {get;set;}
    public datetime date2 {get;set;}
    public string status {
        get{
            if (date1 > dataetime.today)
               return "active";
            else
               return "inactive" ;
        }
    }
}

Upvotes: 1

Related Questions