none
Representing a treelike structure of strings - is this a good approach? RRS feed

  • Question

  • I created a data structure to represent some strings I need to parse.  I just wanted to get someone's opinion on whether or not it is a good approach.  And if you have any suggestion on improving efficiency or even if redesigning it to make use of more advanced feature like inheritance or polymorphism is possible.  (or necessary)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                message m = new message("part1:part1;part2;part3");
    
                //read a value like this
                string obtainValue = m.piecesofMessage[1].partsofPiece[1].ToString();
                
                //change a value like this
                last newlastpart = new last("newpart2");
                m.piecesofMessage[1].partsofPiece[1] = newlastpart;
            }
        }
    
        public class message
        {
    
            public List<piece> piecesofMessage;
            private string _input;
    
            public message(string input)
            {
                _input = input;
    
                piecesofMessage = new List<piece>();
    
                string[] temp = input.Split(':');
                foreach (string s in temp)
                {
                    piece p = new piece(s);
                    this.piecesofMessage.Add(p);
                }
            }
    
            public override string ToString()
            {
                if (piecesofMessage != null)
                {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < piecesofMessage.Count; i++)
                    {
                        sb.Append(piecesofMessage[i].ToString());
                        if (i != piecesofMessage.Count - 1)
                            sb.Append(':');
    
                    }
                    return sb.ToString();
                }
                else
                    return _input;
            }
        }
    
        public class piece
        {
    
            public List<last> partsofPiece;
            private string _input;
    
            public piece(string input)
            {
                _input = input;
                partsofPiece = new List<last>();
                string[] temp = input.Split(';');
                foreach (string s in temp)
                {
                    last l = new last(s);
                    partsofPiece.Add(l);
                }
            }
    
            public override string ToString()
            {
                if (partsofPiece != null)
                {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < partsofPiece.Count; i++)
                    {
                        sb.Append(partsofPiece[i].ToString());
                        if (i != partsofPiece.Count - 1)
                            sb.Append(';');
    
                    }
                    return sb.ToString();
                }
                else
                    return _input;
            }
    
        }
            public class last
            {
    
                public string valueofLast;
    
                public last(string input)
                {
                    valueofLast = input;
                }
                public override string ToString()
                {
                    return valueofLast;
                }
            }
        }
    
    

    Sunday, October 7, 2012 5:14 PM

Answers