0

This is my code

public class SEWorkflow
{
     public Dictionary<SETabs, bool> tabNVPair { get; set; }
}


 public enum SETabs
    {
        Main,
        Attachments,
        TechTeamReview,
        SecurityTeamReview,
        TechTeamImplementation
    }

//I am initialising like this...

 List<SEWorkflow> result = new List<SEWorkflow>()
            {
                  new SEWorkflow()
                {
                     tabNVPair = new Dictionary<SETabs,bool>()
                    {
                          SETabs.Main = true,
                            SETabs.Attachments = true,


                    }
                }
         }

I get an error // //Error CS0131 The left-hand side of an assignment must be a variable, property or indexer

How will I initialise?

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
Venkat
  • 1,702
  • 2
  • 27
  • 47

2 Answers2

5

You were so very close, you missed the square brackets:

List<SEWorkflow> result = new List<SEWorkflow>()
{
    new SEWorkflow()
    {
        tabNVPair = new Dictionary<SETabs,bool>()
        {
            [SETabs.Main] = true,
            [SETabs.Attachments] = true,
        }
    }
};

Or you could use curly braces, like this:

tabNVPair = new Dictionary<SETabs,bool>()
{
    { SETabs.Main, true },
    { SETabs.Attachments, true },
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
-4
 List<SEWorkflow> result = new List<SEWorkflow>()
        {
              new SEWorkflow(), new SEWorkflow(), more entries 
     }

If SEWorkflow constructor requires parameters, you need to provide them:

SEWorkflow(parameter, parameter, more parameters)

To initialize Dictionary:

tabNVPair = new Dictionary<SETabs,bool>() {
    { new SETabs(), true }, { new SETabs(), false }, more KeyValuePair
    }

You need to initialize SETabs accordingly.

rotabor
  • 561
  • 2
  • 10
  • This doesn't answer the OP's question. – Enigmativity Jun 28 '23 at 06:58
  • @Enigmativity at least it's not wrong – rotabor Jun 28 '23 at 07:06
  • @rotabor this code will throw an `ArgumentException` because you will initialize the dictionary with two KeyValuePair with the same key... the SETabs default (SETabs.Main) – Ethan.S Jun 28 '23 at 07:18
  • 1
    @rotabor - It is actually wrong. Your code won't compile, and if you fix the trivial syntax errors, it doesn't run as per Ethan's comments. – Enigmativity Jun 28 '23 at 07:21
  • @Enigmativity My code is not the complete app but it explains how to initialize List and Dictionary. Evidently correct values should be provided for initialization. – rotabor Jun 28 '23 at 07:30
  • 2
    @rotabor - It's close, but so was the OP and the OP did provide a specific examples of the values they were trying to set. When an OP does that it is usually the best to show an example of how to fix their specific code. – Enigmativity Jun 28 '23 at 07:33