我做了一个静态函数,返回一个对象的ArrayList:

 allThread =(ArrayList) AllQuestionsPresented.GetAllThreads();


现在,对象具有我要退出的属性。但我注意到我无法键入allThreads.Name ...或
allThreads [“ Name”]或allThreads [1],它不会给我对象本身。原因intellisense无法识别它。

这是我想要做的..:

该函数属于一类:

    public static ICollection GetAllThreads()
{
    ArrayList allThreads = new ArrayList();
    string findUserID = "SELECT UserID FROM Users";
    string myConnectionString = AllQuestionsPresented.connectionString;

    using (SqlConnection myConnection = new SqlConnection(myConnectionString))
    {
        SqlCommand sqlCommand = new SqlCommand(findUserID, myConnection);
        SqlDataReader reader = sqlCommand.ExecuteReader();
        while (reader.Read())
        {
            AllQuestionsPresented allQ = new AllQuestionsPresented((Guid)reader["UserID"]);
            allThreads.Add(allQ);
        }
    }
    return allThreads;
}


那是另一个类中另一个函数的一些代码:

    forumsPages = new Dictionary<int, List<DisplayAllQuestionsTable>>();
    allThread =(ArrayList) AllQuestionsPresented.GetAllThreads();//I want to convert the ICollection


    for (int i = 0; i < 20; i++)
    {
        threads.Add(new DisplayAllQuestionsTable(allThread[i].//And use it here. I want an object to be returned..same object that was stored in the ArrayList in the static function

    }

最佳答案

使用清单:

 public static List<AllQuestionsPresented> GetAllThreads()
{
    List<AllQuestionsPresented> allThreads = new List<AllQuestionsPresented>();
    string findUserID = "SELECT UserID FROM Users";
    string myConnectionString = AllQuestionsPresented.connectionString;

using (SqlConnection myConnection = new SqlConnection(myConnectionString))
    {
        SqlCommand sqlCommand = new SqlCommand(findUserID, myConnection);
        SqlDataReader reader = sqlCommand.ExecuteReader();
        while (reader.Read())
        {
            AllQuestionsPresented allQ = new AllQuestionsPresented((Guid)reader["UserID"]);
            allThreads.Add(allQ);
        }
    }
    return allThreads;
}

07-25 23:43