0

Hi im using a class having 3 properties where one of the property is an array. I want to add list of data to the class.

public class model
{
    public string Name { get; set; }
    public string Status { get; set; }
    public string[] Actions { get; set; }
}

while Adding value to the above class if the status property has value 'success' I've to add the values 'pause and refresh' for 'Actions'. otherwise I've to add the values 'start and refresh'. when I try like below im getting compile error

model m = new model
            {
                Name = "aaa",
                Status = "success",
                Actions=new string[]{
                  if(status=="success){
                    "Stop",
                    "Restart"
                      }
                }
            };

is it possible to add value based on the condition like above?

user2514925
  • 931
  • 8
  • 33
  • 56
  • 1
    You can do stuff in the getter/setter: https://stackoverflow.com/questions/11159438/looking-for-a-short-simple-example-of-getters-setters-in-c-sharp – VDWWD Jan 13 '19 at 13:28
  • You might consider using an `enum` for `Status`. `string` could be anything, and you have to deal with casing issues, etc. – Rufus L Jan 13 '19 at 14:53

4 Answers4

1

You can use the ternary conditional operator, for example:

Actions = status=="success"
    ? new string[]{ "Stop", "Restart" }
    : new string[]{ "Pause", "Refresh" }
DavidG
  • 113,891
  • 12
  • 217
  • 223
0

Ternary operator usage example:

var status = "success";
model m = new model
{
    Name = "aaa",
    Status = status,
    Actions = status == "success"
        ? new[]
        {
            "Pause",
            "Refresh"
        }
        : new[]
        {
            "Stop",
            "Restart"
        }
};
opewix
  • 4,993
  • 1
  • 20
  • 42
0

You could try this:

Actions = status == "success" 
              ? new string[]{"value"} 
              : new string[] {"another value"};

We use the ternary conditional operator to assingn the proper value to the Actions. When the condition status=="success" is true, the value of new string[]{"value"} assigned to Actions. Otherwise, the value of new string[] {"another value"} assigned to Actions.

Christos
  • 53,228
  • 8
  • 76
  • 108
0

Theoretically, yes, technically, no.

The initialiser is just a proxy for creating a new instance of that struct, calling it's default constructor, and then setting each of those member values. So in theory, it works fine, but syntactically there is no way to access the model at that stage because it hasn't been initialised. You'll need to set that on it's own.

If it's global behaviour, you could write a constructor to take only the name and status, and then fill the actions, but with initialisers the ctor always executes first, so it has no way to access any initialiser data.

Edit, since one is apparently needed; I'm talking about why what the OP was trying to do would not compile. You cannot query other members which were initialised inline. Yes, you can work around it by predeclaring the status variable.

Daedalus
  • 86
  • 10
  • The other answers provide workarounds, I'm explaining why it can't be done inline within an initialiser, what he was trying to do. Obviously you can just declare it on it's own and check that. – Daedalus Jan 13 '19 at 13:34
  • They work in an initialiser with a predeclared variable. I'm saying that you can't declare it as part of the initialiser and then check that in other initialiser statements. It's fine in theory, but the spec doesn't allow it. – Daedalus Jan 13 '19 at 13:36
  • Uh, yes, the other answers suggest doing it with a predeclared variable. He was trying to declare it inline and then check that value inline, even though there is a typo in the question where he didn't capitalise it, given that he doesn't set the status to the same variable. – Daedalus Jan 13 '19 at 13:40