简体   繁体   中英

c# accessing class methods in List<List<T>>

I have a class that I wish to embed in a jagged list. I have created the list as List<T>[] but have been unable to insert or extract data from the list.

In the code below, the line

jaggedList[2].AddSomething("this is a string"); 

generates an error:

"Error CS1061 'List' does not contain a definition for 'AddSomething' and no extension method 'AddSomething' accepting a first argument of type 'List' could be found (are you missing a using directive or an assembly reference?)

The line

jaggedList[i].Finish(); 

produces a similar error message.

There is also a problem with the otheFunctions(ref jaggedList[i], i); line and the compiler error is:

CS1503 Argument 1: cannot convert from 'ref System.Collections.Generic.List' to 'ref ListList.MyList'

but isn't List what jaggesList[I] is? I suspect that this is my problem but don't understand what exactly it is.

class Program
{
    static void Main(string[] args)
    {
        int dynamicallyEstablishedLength = 0;

        dynamicallyEstablishedLength = 4;
        List<MyList>[] jaggedList = new List<MyList>[dynamicallyEstablishedLength];

        for (int i = 0; i < jaggedList.Length; i++)
            jaggedList[i] = new List<MyList>(); //allocate space for each element

        jaggedList[2].AddSomething("this is a string");

        for (int i = 0; i < jaggedList.Length; i++)
            otheFunctions(ref jaggedList[i], i);

        for (int i = 0; i < jaggedList.Length; i++)
            jaggedList[i].Finish();
    }

    void otheFunctions(ref MyList list, int i)
    {
        list.AddSomething("this is string" + i.ToString());
    }
}

public class MyList
{
    private List<string> list;

    public MyList()
    {
        list = new List<string>();
    }

    public void AddSomething(string line, int trace = 0)
    {
        //do some processing on line
        //then
        list.Add(line);
    }

    public void Finish()
    {
        foreach (string s in list)
            Trace.WriteLine(s);
    }
}

the error is clear, because the instance List<MyList>[] jaggedList = new List<MyList>... is of type List<> not type MyList . so few ways to solve this, ie, create an extension method on the type List<> :

public static class ListExtensionMethods
{
    ...

    static public void AddSomething<T>(this List<T> list, string line, int trace = 0)
    {
        //do some processing on line
        //then
        list.Add(line);
    }

    static public void Finish<T>(this List<T> list)
    {
        ...
    }

    ...
}

or, use the type MyList instead of List<> :

MyList[] jaggedList = new MyList[100];
...
jaggedList[0].AddSomething(...)

the second error, is a result of the first error. please search c# extension methods for more info.

You need to understand that in this line jaggedList[2].AddSomething("this is a string"); , jaggedList[2] is a List and you cannot access the methods of MyList directly from it. You can however try something like jaggedList[2][1].AddSomething("this is a string");

mad.meesh had the answer. Changing the code to

MyList[] jaggedList = new MyList[dynamicallyEstablishedLength];
for (int i = 0; i < jaggedList.Length; i++)
    jaggedList[i] = new MyList();

Solved all of my issues. Thanks for all of the comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM