Click here to Skip to main content
14,983,411 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Need help, I have this sources:

C#
public MainDB : List<string>
{
List<string> X;
List<string> Y;
public MainDB()
{
X = new List<string>();
Y = new List<string>();
...??? A ???... //suppose base = new List<string>(); but doesnt works
}
public ...?? B ??... void Add(string Value) //Maybe void abstract or just void or anything, to avoid the conflicts between the base's List method and current method
{
X.Add(Value);
Y.Add(Value);
...??? C ???... //suppose base.Add(Value); but doesnt works
}
}


Anyone know how to solve the A, B, and C? A, B, and C are the base's List<string> I dont know how... Plus, the A suppose to be like this, but it doesnt work :
C#
base = new List<string>();


Here some example:
C#
MainDB test = new MainDB();
test.Add("hi");
test.Add("hello");
Console.WriteLine(test.X[0].ToString());
Console.WriteLine(test.Y[0].ToString());
Console.WriteLine(test[0].ToString());
Console.WriteLine(test.X[1].ToString());
Console.WriteLine(test.Y[1].ToString());
Console.WriteLine(test[1].ToString());


So the result will be:
C#
hi
hi
hi
hello
hello
hello
Posted
Updated 24-Jan-15 6:55am
v7
Comments
Thomas Daniels 24-Jan-15 12:48pm
   
"Anyone know how to solve the A, B, and C?"
What's A, B and C supposed to be?
marhazk 24-Jan-15 12:51pm
   
it's a A, B & C are the base's List<string>
Sergey Alexandrovich Kryukov 24-Jan-15 13:05pm
   
Makes no sense. Base list is the one obtained by inheritance ("this"), but those are other lists. Why?
—SA
Zoltán Zörgő 24-Jan-15 13:01pm
   
Why does it seem to me much like this one: http://www.codeproject.com/Answers/869140/Class-return-overrided-Dictionary-string-object. It starts looking as a homework :(
Sergey Alexandrovich Kryukov 24-Jan-15 13:03pm
   
This is not ArrayList, and this is good, using this obsolete class would be pointless. Just don't call it "ArrayList".
The question "how to solve A, B, C" makes no sense at all. You need to explain what you want to achieve.
—SA

1 solution

Not totally sure what you are asking here, except that you are extending the
C#
List<string>
class and want to basically have 3 lists in one.

I think if this is going to be used in other applications you should extend the
C#
List<T>
class to support generic types.

C#
public MainDB : List<string>
{
    private List<string> m_X;
    private List<string> m_Y;

    // Should have these properties and make the fields private
    public List<string> X {
        get {
            return m_X;
        }
    }
    public List<string> Y {
        get {
            return m_Y;
        }
    }

    /* By calling this constructor, technically the base constructor is already called. If you want to show it being called add the base() call after MainDB() like so*/
    public MainDB():base()
    {
        m_X = new List<string>();
        m_Y = new List<string>();
    }

    /* The List<t> and equally List<string> does not support overriding Add so you must specify the new keyword to essentially hide the base Add.*/
    public new void Add(string Value) 
    {
        m_X.Add(Value);
        m_Y.Add(Value);
        base.Add(Value);
    }
}
   
v3

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