Maxime de Lange
Maxime de Lange

Reputation: 297

Filling in a read-only property with an if statement?

I got a question about a practice test from school.

I need to fill in an application that is already half build (windows forms application)

in this line of code it says the following:

    public bool DangerousLoad 
    { 
        get 
        {
            // ==> Place here the code that has been asked from assignment 1D:

        }
    } 

What they want me to do is this:

Assignment 1D (8points): The property DangerousLoad is not yet done. The property DangerousLoad must get the value true if the ship contains a dangerous load. A load is dangerous if the load is equal to LPG (some sort of gasoline) or Oil. Fill in the property..

So my question is how to do this? because I tried the following:

    public bool DangerousLoad 
    { 
        get 
        {
            bool lpg;
            bool oil;
            // ==> Place here the code that has been asked from assignment 1D:
            if (DangerousLoad == lpg)
            {
                DangerousLoad = true;
            }
        }
    } 

The thing that I don't understand is this.. I need to place the code right under the comment but when I do it like this it gives me an error about the fact that it's a "read-only" property..

so ye.. how could I fix this that if the dangerous load contains LPG or OIL the property turns true?

Thanks in advance

Upvotes: 0

Views: 606

Answers (1)

David Pilkington
David Pilkington

Reputation: 13620

 DangerousLoad = true;

This means that you are trying to set itself. Which you cannot do as it is read onkly. You need to return a value from the "get"

public bool DangerousLoad 
{ 
    get 
    {
        return isLpg || oil;
    }
} 

something like that.

Upvotes: 2

Related Questions