Collection objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jandeerit
    New Member
    • Sep 2008
    • 12

    #1

    Collection objects

    I have a LotMaterial and LotAttributes classes.

    [code=c#]
    public class LotMaterial
    {
    public LotMaterial() { }

    private string _item1;
    public string Item1
    {
    get { return _item1; }
    set { _item1 = value; }
    }

    private string _item2;
    public string Item2
    {
    get { return _item2; }
    set { _item2 = value; }
    }

    }

    public class LotAttributes
    {
    public LotAttributes() {}

    private string _item3;
    public string Item3
    {
    get { return _item3; }
    set { _item3 = value; }
    }

    private string _item4;
    public string Item4
    {
    get { return _item4; }
    set { _item4 = value; }
    }

    private string _item5;
    public string Item5
    {
    get { return _item5; }
    set { _item5 = value; }
    }

    private Collection<LotM aterial> _lotmaterial;
    public Collection<LotM aterial> LotMaterial
    {
    get { return _lotmaterial; }
    set { _lotmaterial = value; }
    }
    }
    [/code]

    In the LotAttributes class, I can have one or more LotMaterial objects.

    Is declaring a Collection<LotM aterial> object in the LotAttributes class the right way to do if I need to access the LotMaterial objects in my client application?

    Thanks in advance..

    -Jan
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Should be alright if the relationship required is composition.

    Comment

    • jandeerit
      New Member
      • Sep 2008
      • 12

      #3
      Originally posted by r035198x
      Should be alright if the relationship required is composition.
      Hi..

      What do you mean "if the relationship required is composition". Can you give more details please? :)

      However, assuming that is the case, can you please post some sample codes on how to access (e.g. Get the values of items1&2) the LotMaterial object from the client application? Let's say I have 3 LotMaterial objects in the LotAttributes class..

      Thanks a lot...,

      -Jan

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        For understanding what composition means better, google "compositio n vs inheritance".
        Assuming your chosen Collection class is ArrayList, you can add objects to it using it's Add method and access them using array like syntax e.g collection[0]

        Read all about it from msdn.

        Comment

        • mldisibio
          Recognized Expert New Member
          • Sep 2008
          • 191

          #5
          At a very basic level, what you are doing is fine.
          Your client would access a LotMaterial property like this:

          Code:
          LotAttributes myLot = GetLot(); // some way to create a LotAttributes object.
          string materialItem2 = myLot.LotMaterial[3].Item2;
          Notice you will have to know the index of the LotMaterial item, unless you add some methods to find by property: e.g.:
          Code:
          string materialItem2 = myLot.FindMaterial("Steel").Item2;
          where FindMaterial() is a method in LotAttributes that searches the LotMaterial collection for a matching item.

          That said, there are alternatives, depending on what you want to achieve.
          The Framework libraries often wrap such collections in a custom collection class to provide some additional abstraction. For example:

          Code:
          public class MaterialsCollection : Collection<LotMaterial>{
            public MaterialsCollection() : base(){}
          }
          
          public class LotAttributes{
            private MaterialsCollection _lotMaterials;
            //...etc
            public MaterialsCollection {get{return this._lotMaterials;}}
          }
          Another option, if you look at LotAttributes as a simple class managing a collection of LotMaterials, then you can simplify your code:
          Code:
          public class LotAttributes : Collection<LotMaterial>
          {
                  public LotAttributes() : base() {}
           
                  private string _item3;
                  public string Item3
                  {
                      get { return _item3; }
                      set { _item3 = value; }
                  }
           
                  private string _item4;
                  public string Item4
                  {
                      get { return _item4; }
                      set { _item4 = value; }
                  }
           
                  private string _item5;
                  public string Item5
                  {
                      get { return _item5; }
                      set { _item5 = value; }
                  }
           
                 public LotMaterial FindMaterialByName(string name){
                   foreach(LotMaterial m in this.Items){
                     if(m.Item1==name) return m;
                   }
                   return null;
                 }
          
          }
          Code:
          LotAttribute myLot = new LotAttribute();
          myLot.Add(new LotMaterial());
          string materialItem2 = myLot.FindMaterialByName("Steel").Item2;
          These are just some ideas of different approaches. Nothing written in stone.

          Comment

          • jandeerit
            New Member
            • Sep 2008
            • 12

            #6
            Originally posted by mldisibio
            At a very basic level, what you are doing is fine.
            Your client would access a LotMaterial property like this:

            Code:
            LotAttributes myLot = GetLot(); // some way to create a LotAttributes object.
            string materialItem2 = myLot.LotMaterial[3].Item2;
            Notice you will have to know the index of the LotMaterial item, unless you add some methods to find by property: e.g.:
            Code:
            string materialItem2 = myLot.FindMaterial("Steel").Item2;
            where FindMaterial() is a method in LotAttributes that searches the LotMaterial collection for a matching item.

            That said, there are alternatives, depending on what you want to achieve.
            The Framework libraries often wrap such collections in a custom collection class to provide some additional abstraction. For example:

            Code:
            public class MaterialsCollection : Collection<LotMaterial>{
              public MaterialsCollection() : base(){}
            }
            
            public class LotAttributes{
              private MaterialsCollection _lotMaterials;
              //...etc
              public MaterialsCollection {get{return this._lotMaterials;}}
            }
            Another option, if you look at LotAttributes as a simple class managing a collection of LotMaterials, then you can simplify your code:
            Code:
            public class LotAttributes : Collection<LotMaterial>
            {
                    public LotAttributes() : base() {}
             
                    private string _item3;
                    public string Item3
                    {
                        get { return _item3; }
                        set { _item3 = value; }
                    }
             
                    private string _item4;
                    public string Item4
                    {
                        get { return _item4; }
                        set { _item4 = value; }
                    }
             
                    private string _item5;
                    public string Item5
                    {
                        get { return _item5; }
                        set { _item5 = value; }
                    }
             
                   public LotMaterial FindMaterialByName(string name){
                     foreach(LotMaterial m in this.Items){
                       if(m.Item1==name) return m;
                     }
                     return null;
                   }
            
            }
            Code:
            LotAttribute myLot = new LotAttribute();
            myLot.Add(new LotMaterial());
            string materialItem2 = myLot.FindMaterialByName("Steel").Item2;
            These are just some ideas of different approaches. Nothing written in stone.

            Hi..

            That's a clear and concise explanation for a newbie like me.. :)

            Thanks a lot guys!

            -Jan

            Comment

            Working...