开发者

Question about dealing with Arraylists in c#

I made a static function that returns me an ArrayList of objects:

 allThread =(ArrayList) AllQuestionsPresented.GetAll开发者_JAVA技巧Threads();

Now the objects have properties that i want to get out. But I noticed that i cant type allThreads.Name...or allThreads["Name"] , or allThreads[1], it wont give me the object itself. Cause intellisense doesnt recognize it..

Here is what I am trying to do..:

That function is in one class:

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

That is some code from another function in another class:

    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

    }


I think what you need to use is a generic version, List<T> where T would be of type AllQuestionsPresented. That should enable IntelliSense for you as you're expecting.

Can you post the definition for AllQuestionsPresented?


USE A List:

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


ArrayList only holds collections of objects; you'd have to cast allThread[i] to an AllQuestionsPresented.

You might look at using generic collections, but you'd probably have to refactor your architecture a bit to handle it.


1) ArrayList contains objects so the property of an object can be accessed without casting the object.

Having a Dictionary full of objects is sort of pointless, I would cast the objects into type that is actually helpful, and has the properties you want. This would require to change the way your select statement works.

Honestly there is no need for the ArrayList, you can write the select statement, to fill the collection you want to use instad.


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

Or if you are sadistic

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


Why use an ARRAYlist? You could just use the much better suited

   List<objecttype here>

With this data structure you can acces everything the way you want (with the square brackets). Even the ICollection thing is quite useless.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜