Parse Array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    Parse Array

    Hello,

    I have the following class ( Level is just a simple enumeration ):

    public class Theme {
    public Subject Subject { get; set; }
    public List<LevelLevel s { get; set; }
    public string Note { get; set; }
    }

    And MyThemes is a List<Theme>

    I am filling "public string[] Themes" from a form and getting:

    Themes[0] = Economy|Base,Su perior|Note 1
    Themes[1] = Math|Base|Note 2

    Now I need to fill MyThemes with these values:

    for (int i = 0; i < this.Themes.Len gth; i++) {

    this.Themes = this.Themes[i].Split(new char[] { '|' },
    StringSplitOpti ons.RemoveEmpty Entries).Select (t =new Theme { Subject
    = ???, Levels = ???, Note = ??? }).ToList();

    }

    I am a little bit confused about this.

    For each Theme, Theme[i] I split it using "|" and then:
    - First value is Subject
    - Second value are the Levels in a CSV format (needs to be converted
    to a list)
    - Third value is the note

    Can't I integrate Linq and Split to get this instead of using more
    loops?

    Thanks,
    Miguel

  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

    #2
    Re: Parse Array

    shapper wrote:
    Hello,
    >
    I have the following class ( Level is just a simple enumeration ):
    >
    public class Theme {
    public Subject Subject { get; set; }
    public List<LevelLevel s { get; set; }
    public string Note { get; set; }
    }
    >
    And MyThemes is a List<Theme>
    >
    I am filling "public string[] Themes" from a form and getting:
    >
    Themes[0] = Economy|Base,Su perior|Note 1
    Themes[1] = Math|Base|Note 2
    >
    Now I need to fill MyThemes with these values:
    >
    for (int i = 0; i < this.Themes.Len gth; i++) {
    >
    this.Themes = this.Themes[i].Split(new char[] { '|' },
    StringSplitOpti ons.RemoveEmpty Entries).Select (t =new Theme { Subject
    = ???, Levels = ???, Note = ??? }).ToList();
    >
    }
    >
    I am a little bit confused about this.
    >
    For each Theme, Theme[i] I split it using "|" and then:
    - First value is Subject
    - Second value are the Levels in a CSV format (needs to be converted
    to a list)
    - Third value is the note
    >
    Can't I integrate Linq and Split to get this instead of using more
    loops?
    >
    Thanks,
    Miguel
    >
    You can't put a list of Theme objects in a string array. Besides, you
    are making a list containing a single object for every iteration of the
    list, so you would overwrite the previous item each time.

    Make a constructor for the Theme class that takes a string, then you can
    use LINQ to create a list of objects from the string array.

    Something like:

    public class Theme {
    public Subject Subject { get; private set; }
    public List<LevelLevel s { get; private set; }
    public string Note { get; private set; }

    public Theme(string data) {
    string[] items = data.Split('|') ;
    Subject = Enum.Parse(type of(Subject), items[0]);
    Levels = new List<Level>();
    foreach (string s in items[1].Split(',')) {
    Levels.Add(Enum .Parse(typeof(L evel), s));
    }
    Note = items[2];
    }

    }

    List<Theme= this.Themes.Sel ect(t =new Theme(t)).ToLis t();

    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    • shapper

      #3
      Re: Parse Array

      On Oct 30, 2:30 pm, Göran Andersson <gu...@guffa.co mwrote:
      shapper wrote:
      Hello,
      >
      I have the following class ( Level is just a simple enumeration ):
      >
        public class Theme {
          public Subject Subject { get; set; }
          public List<LevelLevel s { get; set; }
          public string Note { get; set; }
        }
      >
      And MyThemes is a List<Theme>
      >
      I am filling "public string[] Themes" from a form and getting:
      >
      Themes[0] = Economy|Base,Su perior|Note 1
      Themes[1] = Math|Base|Note 2
      >
      Now I need to fill MyThemes with these values:
      >
         for (int i = 0; i < this.Themes.Len gth; i++) {
      >
         this.Themes = this.Themes[i].Split(new char[] { '|' },
      StringSplitOpti ons.RemoveEmpty Entries).Select (t =new Theme { Subject
      = ???, Levels = ???, Note = ??? }).ToList();
      >
         }
      >
      I am a little bit confused about this.
      >
      For each Theme, Theme[i] I split it using "|" and then:
      - First value is Subject
      - Second value are the Levels in a CSV format (needs to be converted
      to a list)
      - Third value is the note
      >
      Can't I integrate Linq and Split to get this instead of using more
      loops?
      >
      Thanks,
      Miguel
      >
      You can't put a list of Theme objects in a string array. Besides, you
      are making a list containing a single object for every iteration of the
      list, so you would overwrite the previous item each time.
      >
      Make a constructor for the Theme class that takes a string, then you can
      use LINQ to create a list of objects from the string array.
      >
      Something like:
      >
      public class Theme {
          public Subject Subject { get; private set; }
          public List<LevelLevel s { get; private set; }
          public string Note { get; private set; }
      >
          public Theme(string data) {
             string[] items = data.Split('|') ;
             Subject = Enum.Parse(type of(Subject), items[0]);
             Levels = new List<Level>();
             foreach (string s in items[1].Split(',')) {
                Levels.Add(Enum .Parse(typeof(L evel), s));
             }
             Note = items[2];
          }
      >
      }
      >
      List<Theme= this.Themes.Sel ect(t =new Theme(t)).ToLis t();
      >
      --
      Göran Andersson
      _____http://www.guffa.com
      Thanks! It worked fine ... just needed some casting:
      Subject = (Subject)Enum.P arse(typeof(Sub ject), items[0]);
      and
      Levels.Add((Lev el)Enum.Parse(t ypeof(Level), s));

      Comment

      Working...