none
Using a collections as properties in a class RRS feed

  • Question

  • Hi, 

    I'm in the process of converting an old Delphi Windows program to WinForms/C#. The Delphi application relies heavily on a custom class that includes several TStringList objects as properties (pretty similar to a C# string collection).  I've built a c# class that mirrors the old object pascal class, replacing the TStringList objects with StringCollections.  In the Delphi code, the constructor for the class has a number of statements to instantiate the string objects (myStringList := TStringList.Create) while the destructor has the corresponding Free statements.  In my c# code, I've tried to replicate that process.  The string collection objects are declared as private then exposed as public properties with getters and setters.  I can walk through the code in the debugger and see the string collections instantiated properly in the constructor but when I try and use them in an instance of the class, I'm getting a null reference exception.  All the basic data type properties work as expected.  What am I missing?

    Sunday, January 27, 2019 9:31 PM

Answers

  • You can do this now starting with C# 6

    You have auto properties now. 

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

    The private backing collection used for the property that is a collection is instanced when the class is instanced into an object.

    var dtos = new DtoCache(); //all the collection public properties have their private backing collections instanced into objects.

    using System.Collections.Generic;
    
    namespace Entities
    {
        public class DtoCache
        {
            public List<DtoProjectType> ProjectTypes { get; set; } = new List<DtoProjectType>();
            public List<DtoStatus> Statuses { get; set; } = new List<DtoStatus>();
            public List<DtoResource> Resources { get; set; } = new List<DtoResource>();
            public List<DtoDuration> Durations { get; set; } = new List<DtoDuration>();
        }
    }

    Or you instance the private backing collection when the class is instanced into an object.

    var sc = new SomeClass(); // the backing collection has been instanced that the pubic property TheDTOs is using when the class is instanced.

    public class SpecialClass
    {
       private List<DTO> dtos = new List<DTO>();
    
       public List<DTO> TheDTOs
       {
    
         get { return dtos; }
         set { dtos = value; }
       }
    }

    Either way, the backing private collection for the public property collection must be instanced before you address it.

    Or in code and you did none of the above to instance the private backing collection, you would have to do this to instance the collection property into an object within the class before you addressed the collection within the class.

    var sc = new SomeClass();

    sc.TheDTOs = new List<DTO);

    Capiche?


    • Marked as answer by Ray Porter Monday, January 28, 2019 3:38 PM
    Sunday, January 27, 2019 10:10 PM

All replies

  • You can do this now starting with C# 6

    You have auto properties now. 

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

    The private backing collection used for the property that is a collection is instanced when the class is instanced into an object.

    var dtos = new DtoCache(); //all the collection public properties have their private backing collections instanced into objects.

    using System.Collections.Generic;
    
    namespace Entities
    {
        public class DtoCache
        {
            public List<DtoProjectType> ProjectTypes { get; set; } = new List<DtoProjectType>();
            public List<DtoStatus> Statuses { get; set; } = new List<DtoStatus>();
            public List<DtoResource> Resources { get; set; } = new List<DtoResource>();
            public List<DtoDuration> Durations { get; set; } = new List<DtoDuration>();
        }
    }

    Or you instance the private backing collection when the class is instanced into an object.

    var sc = new SomeClass(); // the backing collection has been instanced that the pubic property TheDTOs is using when the class is instanced.

    public class SpecialClass
    {
       private List<DTO> dtos = new List<DTO>();
    
       public List<DTO> TheDTOs
       {
    
         get { return dtos; }
         set { dtos = value; }
       }
    }

    Either way, the backing private collection for the public property collection must be instanced before you address it.

    Or in code and you did none of the above to instance the private backing collection, you would have to do this to instance the collection property into an object within the class before you addressed the collection within the class.

    var sc = new SomeClass();

    sc.TheDTOs = new List<DTO);

    Capiche?


    • Marked as answer by Ray Porter Monday, January 28, 2019 3:38 PM
    Sunday, January 27, 2019 10:10 PM
  • Hi Ray Porter,

    Thank you for posting here.

    Based on my search, please check the examples in the link below.

    https://stackoverflow.com/questions/8196117/c-sharp-get-and-set-properties-for-a-list-collection

    https://stackoverflow.com/questions/4178570/how-do-create-a-class-property-that-is-a-collection-of-another-class

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 28, 2019 2:10 AM
    Moderator
  • Thanks.  This seems to have solved my issue.
    Monday, January 28, 2019 3:38 PM