Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create an AugmentedStringList class, List<string> class with a few members describing the list as a whole. Obviously, I can make a class with a List<string> member, and map all operations on the lsit (such as Add, Remove) from AugmentedStringList to that List<string> member. But the C# compiler allows me to subclass List<string>, making all those operations directly available:

C#
public class AugmentedStringList : List<string> {
  public string language {get; set; }

  public AugmentedStringList(string lang, string initialStrings) {
    language = lang;
    if (string.IsNullOrEmpty(initialStrings)) <baseconstructor>()
    else <baseconstructor>(initialStrings.Split(';');    
  }
}


There is more to the AugmentedStringList, but this illustrates my problem: How do I refer to the base List<string>? No, I cannot set the list value using 'this'; it is read only, and it also refers to the augmented structure, not the base string list. Nor can I assign to 'base'.

If I could always call the same base constructor, I could of course do it the ordinary way, adding : base(...), but that doesn't work in this case. There are lots of other operations, too, where I need to refer directly to the simple list, and 'this' doesn't work.

Is there any way I can refer to the simple string list from the class methods, or do I have to use a List<string> member and write stubs for all the relevant operations?
Posted
Updated 9-Jan-13 1:18am
v2

1 solution

this works in all cases I can think of. For example, you can access the list directly by index, or use the base class methods:
C#
public class MyList : List<string>
    {
    public string GetAt(int index)
        {
        return this[index];
        }
    public void AddMe(string s)
        {
        Add(s);
        }
    }

What are you trying to do that doesn't work?


"One of the things I would like to do is illustrated in my first description: Do some processing of constructor parameters to initialize the list member.

Another thing (not illustrated in my code) is to extract the list element itself, without the augmentations (in the form of extra parameters), like an AugmentedStringList.GetSimpleList() function, similar to AugmentedStringList.GetLanguage(). How do I write GetSimpleList() - how can I refer to the base class list?"



You can't call a base constructor from within the inherited class constructor - the base construction has to be complete before the inherited constructor code is executed, or it could try to use uninitialized memory. So what you are trying to do can't be done. But, you could create two constructors in the inherited class (one with a single param, and one with two) that reference the base constructors appropriately:
C#
public string Lang { get; set; }
public MyList(string lang)
    {
    Lang = lang;
    }
public MyList(string lang, string initialStrings)
    : base(initialStrings.Split(';'))
    {
    Lang = lang;
    }

To return the base class is easy: your class instance is an instance of the base class:
C#
public List<string> GetSimpleList()
    {
    return this;
    }
But to be honest you don't need to do that, because:
C#
MyList ml = new MyList("English", "hello;hello again");
List<string> ls = ml;
Will work fine anyway!
 
Share this answer
 
v2
Comments
trønderen 9-Jan-13 6:30am    
One of the things I would like to do is illustrated in my first description: Do some processing of constructor parameters to initialize the list member.

Another thing (not illustrated in my code) is to extract the list element itself, without the augmentations (in the form of extra parameters), like an AugmentedStringList.GetSimpleList() function, similar to AugmentedStringList.GetLanguage(). How do I write GetSimpleList() - how can I refer to the base class list?
OriginalGriff 9-Jan-13 7:02am    
Answer updated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900