2011-06-07 46 views
1

我做了一個靜態函數返回我對象的ArrayList:問題有關與的ArrayList在C#中處理

allThread =(ArrayList) AllQuestionsPresented.GetAllThreads(); 

現在有對象,我想出去性質。但我注意到我不能輸入allThreads.Name ...或者allThreads [「Name」]或allThreads [1],它不會給我這個對象本身。原因智能感知犯規承認它..

這裏就是我想要做..:

這個函數是一個類:

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 

    } 

回答

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

感謝您的代碼 – Matrix001 2011-06-07 18:14:45

0

爲什麼要使用ARRAYlist?你可以只使用了更適合

List<objecttype here> 

有了這些數據結構,你可以存取權限的一切你想要的方式(用方括號中)。即使是ICollection也沒用。

+0

很好的建議。謝謝 – Matrix001 2011-06-07 18:14:32

2

我認爲你需要使用的是一個通用版本,List<T>其中T將是AllQuestionsPresented類型。這應該可以爲你啓用IntelliSense,就像你期待的那樣。

你能發表關於AllQuestionsPresented的定義嗎?

+0

這是一個有屬性的類..例如。公共字符串名稱,公共字符串姓氏,公共int age..nothing重要 – Matrix001 2011-06-07 18:05:44

1

ArrayList只保存對象的集合;你必須將AllThread [i]轉換爲AllQuestionsPresented。

你可能會考慮使用泛型集合,但是你可能不得不重構你的架構來處理它。

+0

oooh,我看..我最好訴諸ArrrayList – Matrix001 2011-06-07 18:06:18

1

1)ArrayList包含對象,所以對象的屬性可以在不鑄造對象來訪問。

讓Dictionary充滿對象是毫無意義的,我會將對象轉換爲實際上有用的類型,並且具有您想要的屬性。這將需要改變你的select語句的工作方式。

老實說,不需要ArrayList,你可以編寫select語句來填充你想要使用instad的集合。

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