Robert Omete
Robert Omete

Reputation: 64

conditional setter in C#

I am using Visual Studio 2012 C#

I am trying to return a result to a setter in a class based on an enum choice selected by a user. The enum is as below:

public TransactionType transactiontypes
    {
        get
        {
            return _Transactiontypes;
        }
        set
        {
            SetPropertyValue("transactiontypes", ref _Transactiontypes, value);
        }
    }

public enum TransactionType:byte
{
    Unknown = 0, CaseFile = 2, Litigant = 3, Court = 4
}

So I created a class that uses a condition based on the enum choice:

 public Decimal TransactCredit
    {
        get
        {
            return _TransactCredit;
        }

        if (TransactionType = 1)

        set

        {
            SetPropertyValue("TransactCredit", ref _TransactBalance, value);
        }

        else set
        {
            SetPropertyValue("TransactCredit", ref _TransactCredit, value);
        }

    }



    public Decimal TransactBalance
    {
        get
        {
            return _TransactBalance;
        }
        set
        {
            SetPropertyValue("TransactBalance", ref _TransactBalance, value);
        }
    }

I keep getting an error that highlights the "If" word requires a "get or set accessor". How would I correctly write the above conditional statements?

Upvotes: 1

Views: 9141

Answers (2)

Matthew Haugen
Matthew Haugen

Reputation: 13286

The compiler reads your get and set on a property and effectively rewrite those into two methods: TransactCredit_getter() and TransactCredit_setter(Decimal Value). As a result, putting a condition outside of the boundaries of either keyword makes no sense. That would be like writing your own methods and attempting to apply some sort of condition on which should be run, in the class definition. Rather, you should do your test inside your set operation.

public Decimal TransactCredit
{
    get
    {
        return _TransactCredit;
    }
    set
    {
        if (TransactionType == 1)
        {
            SetPropertyValue("TransactCredit", ref _TransactBalance, value);
        }
        else
        {
            SetPropertyValue("TransactCredit", ref _TransactCredit, value);
        }
    }
}

Although in fairness, and I say this without knowing everything about your code or the application, this seems like a time when your design might be losing readability. Are you sure this is the best solution for your scenario?

Upvotes: 4

Tilak
Tilak

Reputation: 30698

Your code has syntax errors. Try following instead

    public Decimal TransactCredit
    {
        get
        {
            return _TransactCredit;
        }



        set
        {
            if (TransactionType == 1)
            {
                SetPropertyValue("TransactCredit", ref _TransactBalance, value);
            }

            else 
            {
                SetPropertyValue("TransactCredit", ref _TransactCredit, value);
            }
        }

    }

Upvotes: 1

Related Questions