关于在c#中处理Arraylists的问题

时间:2011-06-07 17:54:19

标签: c# asp.net collections arraylist

我创建了一个静态函数,它返回一个对象的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

    }

6 个答案:

答案 0 :(得分:2)

我认为您需要使用的是通用版本,List<T>其中T的类型为AllQuestionsPresented。这应该像你期望的那样为你启用IntelliSense。

您可以发布AllQuestionsPresented的定义吗?

答案 1 :(得分:1)

ArrayList仅包含对象集合;你必须将allThread [i]转换为AllQuestionsPresented。

您可能会考虑使用泛型集合,但您可能需要稍微重构一下您的体系结构才能处理它。

答案 2 :(得分:1)

使用清单:

 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;
}

答案 3 :(得分:1)

1)ArrayList包含对象,因此可以在不转换对象的情况下访问对象的属性。

让一个字典充满对象是没有意义的,我会将对象转换为实际有用的类型,并具有您想要的属性。这需要改变select语句的工作方式。

老实说,不需要ArrayList,你可以编写select语句,来填充你想要使用的集合。

答案 4 :(得分:1)

或使用LINQ

[Table(Name="Users")]
class User
{
   [Column]
   public Guid UserId;
}  

IEnumerable<User> questions;
using (SqlConnection myConnection = new SqlConnection(myConnectionString))
{
    var dc = new DataContext(myConnection);
   // Use ToArray to force all reads on the connection
    questions = 
        (from user in dc.GetTable<User>() 
        select new AllQuestionsPresented(user.UserId)).ToArray()
}

var threads = 
       from question in questions 
       select new DisplayAllQuestionsTable(question.SomeProperty);

或者如果你是虐待狂

var threads = 
       from question in (
            from user in dc.GetTable<User>() 
            select new AllQuestionsPresented(user.UserId) )
       select new DisplayAllQuestionsTable(question.SomeProperty); 

答案 5 :(得分:0)

为什么要使用ARRAYlist?你可以使用更合适的

   List<objecttype here>

使用此数据结构,您可以按照您想要的方式访问所有内容(使用方括号)。即使ICollection也没用。