Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. List inside a struct

List inside a struct

Scheduled Pinned Locked Moved C#
helptutorialquestion
7 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Islorvat
    wrote on last edited by
    #1

    Hello, Can anyone tell me how to use a List of strings inside a struct? I get the 'Object reference not set to an instance of an object.' error.

        public Form1()
        {
            InitializeComponent();
        }
    
        struct testStruct
        {
            public List<string> sList
            {
                get;
                set;
            }
    
        }
    
        private void Form1\_Load(object sender, EventArgs e)
        {
            testStruct aTestStruct = new testStruct();
            aTestStruct.sList.Add("test");
        }
    

    Thank you!

    J D 2 Replies Last reply
    0
    • I Islorvat

      Hello, Can anyone tell me how to use a List of strings inside a struct? I get the 'Object reference not set to an instance of an object.' error.

          public Form1()
          {
              InitializeComponent();
          }
      
          struct testStruct
          {
              public List<string> sList
              {
                  get;
                  set;
              }
      
          }
      
          private void Form1\_Load(object sender, EventArgs e)
          {
              testStruct aTestStruct = new testStruct();
              aTestStruct.sList.Add("test");
          }
      

      Thank you!

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      You've not instantiated the List anywhere. This has nothing to do with it being inside a struct - the same would have happened if your list was inside a class. You can fix this by first creating the List.

      testStruct aTestStruct = new testStruct();
      aTestStruct.sList = new List<string>();
      aTestStruct.sList.Add("test");

      However, a few pointers 0.1) Naming convention for C# would instruct that your property is called List instead of sList. 0.2) Be careful using structs, they have some hard to understand facets - if in doubt use a class. 0.3) Strictly speaking, a List should generally not be exposed by a public property. Consider whether the struct/class should instead expose just the interface, such as an Add method to add to an internal List.

      1 Reply Last reply
      0
      • I Islorvat

        Hello, Can anyone tell me how to use a List of strings inside a struct? I get the 'Object reference not set to an instance of an object.' error.

            public Form1()
            {
                InitializeComponent();
            }
        
            struct testStruct
            {
                public List<string> sList
                {
                    get;
                    set;
                }
        
            }
        
            private void Form1\_Load(object sender, EventArgs e)
            {
                testStruct aTestStruct = new testStruct();
                aTestStruct.sList.Add("test");
            }
        

        Thank you!

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        structs are meant for small value types. Both List<T> and string are reference types, and their size could potentially be very large, so I would never use them inside a struct.

        Dave
        Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        L 1 Reply Last reply
        0
        • D DaveyM69

          structs are meant for small value types. Both List<T> and string are reference types, and their size could potentially be very large, so I would never use them inside a struct.

          Dave
          Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          But since they are reference types, the struct would only contain the reference.. "The actual thing" is included in the struct if it's marked ByValArray or ByValTStr or when it is a fixed size buffer[^] (I've never actually seen one of those)

          D 1 Reply Last reply
          0
          • L Lost User

            But since they are reference types, the struct would only contain the reference.. "The actual thing" is included in the struct if it's marked ByValArray or ByValTStr or when it is a fixed size buffer[^] (I've never actually seen one of those)

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            True, I hadn't actually considered that! I still don't like it due to the mutability implied by using a reference type inside a struct, especially with List<T> as the contents would be mutable even if the exposed list wasn't. A ReadOnlyCollection<T> would be OK I suppose, something like this wrapper...

            public struct ExampleStruct
            {
            private List<string> list;

            public ExampleStruct(IEnumerable<string> strings)
            {
                if (strings == null)
                    list = null;
                else
                    list = new List<string>(strings);
            }
            
            public ReadOnlyCollection<string> List
            {
                get
                {
                    if (list == null)
                        return null;
                    return list.AsReadOnly();
                }
            }
            

            }

            Dave
            Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

            L 1 Reply Last reply
            0
            • D DaveyM69

              True, I hadn't actually considered that! I still don't like it due to the mutability implied by using a reference type inside a struct, especially with List<T> as the contents would be mutable even if the exposed list wasn't. A ReadOnlyCollection<T> would be OK I suppose, something like this wrapper...

              public struct ExampleStruct
              {
              private List<string> list;

              public ExampleStruct(IEnumerable<string> strings)
              {
                  if (strings == null)
                      list = null;
                  else
                      list = new List<string>(strings);
              }
              
              public ReadOnlyCollection<string> List
              {
                  get
                  {
                      if (list == null)
                          return null;
                      return list.AsReadOnly();
                  }
              }
              

              }

              Dave
              Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              A field in a struct is mutable though, so I'm not sure what you mean..?

              D 1 Reply Last reply
              0
              • L Lost User

                A field in a struct is mutable though, so I'm not sure what you mean..?

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                Of course, but they are generally exposed only by a read-only property. It's rare and generally not recommended to have a property with a setter or to expose the field directly in a struct.

                Dave
                Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups