1

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

 allThread =(ArrayList) AllQuestionsPresented.GetAllThreads();

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

    }

6 Answers 6

2

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?

Sign up to request clarification or add additional context in comments.

1 Comment

It is a class with properties..e.g. public string Name, public string Surname, public int age..nothing important
1

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

Comments

1

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 Comment

oooh, i see.. i would better resort to ArrrayList
1

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.

Comments

1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.