Reputation: 1272
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
}
Upvotes: 1
Views: 181
Reputation: 13628
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);
Upvotes: 1
Reputation: 2551
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.
Upvotes: 1
Reputation: 2338
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;
}
Upvotes: 1
Reputation: 354
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.
Upvotes: 1
Reputation: 50855
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
?
Upvotes: 2
Reputation: 751
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.
Upvotes: 0