Click here to Skip to main content
11,438,226 members (56,176 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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:

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? 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 member and write stubs for all the relevant operations?
Posted 9-Jan-13 0:13am
Edited 9-Jan-13 2:18am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
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:
        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:
        public List<string> GetSimpleList()
            {
            return this;
            }
But to be honest you don't need to do that, because:
            MyList ml = new MyList("English", "hello;hello again");
            List<string> ls = ml;
Will work fine anyway!
  Permalink  
v2
Comments
Member 8575121 at 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 at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.150506.1 | Last Updated 9 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100