0

I'm trying to check if GroupEmails is null before executing this section of my code. However, whenever I am checking for the null value, it returns an error Cannot resolve symbol GroupEmails Is there a better solution for this?

Code

Group = new DB.Group
{
    GroupPhones = groupPhones,

    GroupEmails = new List<DB.GroupEmail>
    {
        new DB.GroupEmail
        {
            Email = groupt.Email
        }
    }
}

Trying to check if null like the following:

Group = new DB.Group
{
    GroupPhones = groupPhones,

    GroupEmails == null ? null : new List<DB.GroupEmail>
    {
        new DB.GroupEmail
        {
            Email = groupt.Email
        }
    }
}

Modified Solution Thanks to @Habib

Group = new DB.Group
{
    GroupPhones = groupPhones,

    GroupEmails = group.Email == null ? null : new List<DB.GroupEmail>
                                               {
                                                  new DB.GroupEmail
                                                  {
                                                    Email = groupt.Email
                                                  }
                                                }
}
2
  • Where did you check if groupt is null or not? Commented Jun 5, 2015 at 14:20
  • If this did work you're saying if it is null, then set it to null. Which wouldn't make any sense. Commented Jun 5, 2015 at 14:22

2 Answers 2

1

There is no need to check for null in object initializer.

 GroupEmails == null ? null : new List<DB.GroupEmail>

GroupEmails property is being initialized in the object initializer for new DB.Group.

This shouldn't even compile as you can't perform checks like these on properties/fields in object initializer.

If you want to initialize your property GroupEmail irrespective of data then you can do:

Group = new DB.Group
{
    GroupPhones = groupPhones,
    GroupEmails = new List<DB.GroupEmail>(),
}

or if you want to compare group.Email to null, then you can do:

GroupEmails = group.Email == null ? null : new List<DB.GroupEmail>
                                          {
                                            new DB.GroupEmail
                                            {
                                              Email = groupt.Email
                                            }
                                          }
Sign up to request clarification or add additional context in comments.

4 Comments

The issue I am running into is that it generates an error while running if I don't pass in any data for the GroupEmails. How could I fix that?
@CodeChaser If that's the problem then show that code and the error message you are getting.
@CodeChaser, your first code snippet should work fine as long as you have some valid value in groupt.Email , otherwise you can simply initialize the GroupEmails like ` GroupEmails = new List<DB.GroupEmail>(),` and leave the List initialization.
@Habib you are right, no need to check for a null value on GroupEmails. I ended up changing it to look like the following: GroupEmails = group.Email == null ? null : new List<DB.GroupEmail>
1

You're creating the object, so I'm not sure why you would check for null...

var group = new DB.Group();
group.GroupPhones = groupPhones;
group.GroupEmails = new List<DB.GroupEmail>();
group.GroupEmails.Add(new DB.GroupEmail() { Email = groupt.Email });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.