locked
Help with an immutable class RRS feed

  • Question

  • User-1269653725 posted

    I need to fill an object list with stuff, and later on i want it immutable for a certain part of a code that modifies it in a loop.

    What i have NOW, cannot do the job because i am ignorant to the immutable concept, i have never used it before, i need it now, however.

    how do i turn my code and re-work it so that it's mutable to fill data, and later on immutable for only a certain kind of code?

    Here's my code:

      public List<UBTDataTable> UBTDataList { get; set; }
    
            public class UBTDataTable
            {
                public decimal BALANCE { get; set; }
                public int TIME { get; set; }
                public string EXPIRATION { get; set; }
                public int EXPIRATIONTIME { get; set; }
                public int MONTH { get; set; }
                public int DAY { get; set; }
                public int YEAR { get; set; }
    
    
    
            }



    ----------------------------------------------------

    Here is what i looked on google but i dont know what it means:

     public class Writer1
        {
            // Read-only properties.   
            public string Name { get; }
            public string Article { get; private set; }
     
            // Public constructor.   
            public Writer1(string authorName, string articleName)
            {
                Name = authorName;
                Article = articleName;
            }
        }

    Thursday, December 12, 2019 7:09 AM

All replies

  • User303363814 posted

    i want it immutable for a certain part of a code that modifies it in a loop

    immutable means that something cannot be changed.  You seem to be asking for something that cannot be changed to be changed

    Do you want it immutable or do you want to modify it?  Can't have both.

    Also, when you are thinking about your design you should be clear about what whether you want an immutable list and/or immutable elements of the list.

    That is do you want to stop clients adding/removing entries from UBTDataList (have a look at ReadOnlyCollection<T>)

    … or do you want to stop clients changing some/all of the properties of elements of the list.  Think about changing the setter access for particular properties.

    Thursday, December 12, 2019 8:30 AM
  • User-1269653725 posted

    How do i explain this? 

    I want to build up my list with values and in so modify them here and there.

    Next, i want to test the values by modifying/reading them, but the test ends up with the modified results.

    I want to create my list of values, test them by modifying them again, BUT, keep the original values AFTER the test, does that make sense?

    Thursday, December 12, 2019 9:56 AM
  • User753101303 posted

    Hi,

    The idea is that you can only define property values when you create an object. So technically speaking you won't modify an existing object but you'll create a new object with updated values... ie :

    var a=new Writer1("Verlaine","How I wrote Les Misérables");
    a.Name="Hugo"; // doesn't work as the property is read only

    var b=new Writer1("Hugo",a.Article); // works, you have to create a new object (possibly still keeping a reference to the old one)...

    Is this enough ? You should find articles but from a quick search I find either basic stuff or maybe overkill libraries...

    Edit: if for code testing you could also consider using a method that returns a new know list and restart from that for each test or to clone a source list etc...

    Thursday, December 12, 2019 10:37 AM
  • User303363814 posted

    Once you are happy with the state of your objects you can create a new set of objects which can be altered without affecting the originals.  A 'copy constructor' is quite useful.

    The idea is that you have a constructor which takes an existing object as the parameter and produces a new object with property values that are all the same.

    Add a constructor to your class

    public class UBTDataTable
    {
         public decimal BALANCE { get; set; }
         public int TIME { get; set; }
         public string EXPIRATION { get; set; }
         public int EXPIRATIONTIME { get; set; }
         public int MONTH { get; set; }
         public int DAY { get; set; }
         public int YEAR { get; set; }
    
        public UBTDataTable(UBTDataTable original) {
            BALANCE = original.BALANCE;
            TIME = original.TIME;
            EXPIRATION = original.EXPIRATIOn;
            MONTH = original.MONTH;
            DAY = original.DAY;
            YEAR = original.YEAR;
        }
    }

    Once UBTDataList is in the state that you want then you can easily create a copy

    var myPlayground = UBTDataList.Select(d => new UBTDataTable(d));

    You can change myPlayground and it's elements as much as you want but the elements of UBTDataList will not be affected.

    Friday, December 13, 2019 6:33 AM