none
how do I update the original value of an object member when a sub object to that member value changes? RRS feed

  • Question

  • Sorry if my question doesn't make sense, wasn't sure how to ask it.

    Basically what I am trying to do is create classes to represent strings of strings of strings of.... where the initial string is a series of strings delimited by separators

    example "test1|test2|test3|test4|"

    Starting with just a main string and sub fields I cam up with this

    public sealed class FlatFileMessage
        {
            private string message = string.Empty;
            private char fieldSeparator = '|'; //default
    
            public string Message
            {
                get { return message; }
                set { message = value; }
            }
    
            public char FieldSeparator
            {
                get { return fieldSeparator; }
                set { fieldSeparator = value; }
            }
    
            public List<FlatFileField> Fields;
            private ParseFlatFile p;
    
            public FlatFileMessage(string line)
            {
                Message = line;
                Fields = new List<FlatFileField>();
    
                p = new ParseFlatFile(this);
            }
    
    
    
        }
    
        public sealed class FlatFileField
        {
            private string field = string.Empty;
    
            public string Field
            {
                get { return field; }
    
                set { this.field = value; }
            }
    
            public FlatFileField(string field)
            {
                Field = field;
            }
        }
    
        public sealed class ParseFlatFile
        {
            private FlatFileMessage FFm { get; set; }
    
            public ParseFlatFile(FlatFileMessage ffm)
            {
                FFm = ffm;
                Parse(FFm);
            }
    
            public void Parse(FlatFileMessage flatfilemessage)
            {
                //parse
                string[] fields = flatfilemessage.Message.Split(flatfilemessage.FieldSeparator);
                for (int i = 0; i < fields.LongLength; i++)
                {
                    FlatFileField f = new FlatFileField(fields[i]);
                    flatfilemessage.Fields.Add(f);
                }
            }
        }
    
        public sealed class AssembleFlatFile
        {
            public static string Assemble(FlatFileMessage ffm)
            {
                string message = string.Empty;
                bool first = true;
                for (int i = 0; i < ffm.Fields.Count; i++)
                {
                    if (!first)
                        message += ffm.FieldSeparator + ffm.Fields[i].Field;
                    else
                        message += ffm.Fields[i].Field;
                }
    
                return message;
            }
        }

    My question is this

     I want to be able to change the value of the sub strings within the original string, but when I change a value flatfilemessageobject.Fields[4].Field = "changed text";

    I want it to update flatfilemessageobject.Message automatically when a change is made to one of the sub strings.  I made an assemble function but not sure where to put it.

    Also not sure if I should just create another object that represent the changed string, and keep the original unmodified or is that inneficient?

    Any help is greatly appreciated

    thanks

    Saturday, July 21, 2012 3:10 AM

Answers

  • If you need to store the original then it's probably worth it to store the original. Perhaps for each field have a current version and an original version.

    While it might be overkill you can have your class implement INotifyPropertyChanged and have the different fields listen to changed events from the other fields.

    Saturday, July 21, 2012 3:30 AM