-1

Let's say i have a Class called Field

public class Field
{
    public string name {get;set;}
    public string type {get;set;}

}

For my program, i would need to build a List, but some Field in the List are general and would duplicate in few part of program, I would like to separate those Field that will be duplicated into a base class so that those sub classes can inherit from the base class without having some Field duplicated. The concept is roughly as below:

base classA {
    List<Field> list = new List<Field>();
    list.add(new Field(){name = "fieldNameA", type = "typeA"});
    list.add(new Field(){name = "fieldNameB", type = "typeB"});
    list.add(new Field(){name = "fieldNameC", type = "typeC"});
}

base classB {
    List<Field> list = new List<Field>();
    list.add(new Field(){name = "fieldNameX", type = "typeX"});
    list.add(new Field(){name = "fieldNameY", type = "typeY"});
    list.add(new Field(){name = "fieldNameZ", type = "typeZ"});
}


sub class {

   private void methodA() {
      //Inherits list initialized at 
      //     **base classA** above, 
      //and continues to initialize some other Fields

     list.add(new Field(){name = "fieldNameD", type = "typeD"});
     list.add(new Field(){name = "fieldNameE", type = "typeE"});

      //so at here finally i would have list which consists of fieldNameA to fieldName E
    }


    private void methodB() {
      //Inherits list initialized at 
      //      **base classB** above, 
      //and continues to initialize some other Fields

     list.add(new Field(){name = "fieldNameD", type = "typeD"});
     list.add(new Field(){name = "fieldNameE", type = "typeE"});

      //so at here finally i would have list which consists of fieldNameX, Y,Z,D,E
    }
}

How am i supposed to do in order to achieve this?

Mentos
  • 13
  • 6
  • Code you've shown (when updated to correct C# syntax) should do exactly what you need. Please clarify what you have problem with. Also try to update tile so it does not look like you need to inherit from `List` but rather something else (possibly "how to use field from base class"). – Alexei Levenkov Dec 11 '15 at 06:51
  • Hi Alexei, I solved the problem just now. I have updated my question, now im having problem with multiple inheritance – Mentos Dec 11 '15 at 07:24
  • Instead of clarifying you've completely changed the question. Changing question completely is not really welcome one SO, especially if original question was answered. Check out [Chameleon questions](http://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions) for more guidance (summary - rollback edit and ask new question). – Alexei Levenkov Dec 11 '15 at 17:26

1 Answers1

1

I'm not sure if you mean composition like this:

class BaseClass {
    protected List<Field> list = new List<Field>();
    public BaseClass()
    {           
       list.add(new Field(){name = "fieldNameA", type = "typeA"});
       list.add(new Field(){name = "fieldNameB", type = "typeB"});
       list.add(new Field(){name = "fieldNameC", type = "typeC"});
    }
}

class SubClass : BaseClass {
    public SubClass() : base()  
    {           
       list.add(new Field(){name = "fieldNameD", type = "typeD"});
       list.add(new Field(){name = "fieldNameE", type = "typeE"});
    }
}
Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • Hi Mike, Thanks. it works like a charm.. I have another question, What if i have 2 base classes and different methods would inherit from either one base class? I have edited my question accordingly – Mentos Dec 11 '15 at 07:04
  • @Mentos - C# does not support multiple inheritance so depending on what it is exactly that you're trying to achieve you may need to explore composition (ie. your sub class is not really a sub class, but it has dependencies on a couple of other classes that is holds instances of internally) or using interfaces of which your classes can implement multipel of (but that won't give you the ability to have lists that contain elements the way you had in your original question); good luck - perhaps ask a different question and state what your end goal is, from an architecture perspective – Mike Dinescu Dec 12 '15 at 22:33