0

Why cannot I use if else statement inside this local variable account? Inside the "if" I'm planning to enter the code IsActive = "Yes", then inside the "else" IsActive = "No". But the IsActive string that I declared in the class is not appearing/suggesting.

private void btnAdd_Click(object sender, EventArgs e)
{
    UserAccount account = new UserAccount()
    {
        FirstName = txtFirstName.Text,
        LastName = txtLastName.Text,
        Email = txtEmail.Text,
        TeamName = txtTeamName.Text,
        Password = txtPassword.Text,
        // IsActive = "Yes",
        if (chkIsActive.Checked == true)
        {
        }
        else
        {
        }
        UserId = int.Parse(txtUserId.Text)
    };
}
  • 3
    You cannot use an `if` statement in an [object initializer](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers) because, to quote [Eric Lippert](https://stackoverflow.com/users/88656/eric-lippert), *[Features are unimplemented by default; C# does not have that feature because no one designed, implemented and shipped the feature to customers.](https://stackoverflow.com/a/8673015/3744182)* Answers below show some workarounds. – dbc Jun 05 '22 at 00:22

3 Answers3

3

You can't use an if else statement inside an object initialization. You can only assign to public writable (that is, properties having a setter) properties there. As suggested in one comment below, starting from C# 9 you can also set init-only properties when using object initialization syntax. These properties can be written only during object construction, once the object construction phase is completed you are not allowed to write the property. Regardless of the C# language version, you can't use any control flow statement in object initialization.

A working version of your code is the following:

    string isActive = chkIsActive.Checked ? "Yes": "No";

    var account = new UserAccount()
    {

        FirstName = txtFirstName.Text,
        LastName = txtLastName.Text,
        Email = txtEmail.Text,
        TeamName = txtTeamName.Text,
        Password = txtPassword.Text,
        IsActive = isActive,
        UserId = int.Parse(txtUserId.Text)
    };
Enrico Massone
  • 6,464
  • 1
  • 28
  • 56
  • Also you can initialize `init` properties, such as `public string FirstName { get; init; }` which aren't strictly public writable properties. – Enigmativity Jun 05 '22 at 00:21
1

You are trying to use control statement inside the class initializer. You cannot do that.

  1. You can use ternary operator.
UserAccount account = new UserAccount()
{

    FirstName = txtFirstName.Text,
    LastName = txtLastName.Text,
    Email = txtEmail.Text,
    TeamName = txtTeamName.Text,
    Password = txtPassword.Text,
    //IsActive = "Yes",
    IsActive = chkIsActive.Checked ? "Yes" : "No",
    UserId = int.Parse(txtUserId.Text)
};

  1. Don't use boolVariable == true, just use boolean itself. Only exception is a nullable boolean.
Oybek
  • 7,016
  • 5
  • 29
  • 49
-1

You can do this a few ways, the following is an example of using a ternary condition to set the property value:

private void btnAdd_Click(object sender, EventArgs e)
{

    UserAccount account = new UserAccount()
    {
        FirstName = txtFirstName.Text,
        LastName = txtLastName.Text,
        Email = txtEmail.Text,
        TeamName = txtTeamName.Text,
        Password = txtPassword.Text,
        IsActive = chkIsActive.Checked ? "Yes" : "No",
        UserId = int.Parse(txtUserId.Text)
    };
emagers
  • 841
  • 7
  • 13