Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing objects in ArrayList 2

Status
Not open for further replies.

kav123

Programmer
Jan 12, 2005
210
0
0
GB
Can anyone tell me the best method to pick a record from a database using ExecuteReader, and passing it into an arraylist or any collection. I basically want to store all the records into a collection, arraylist or dictionary and then use that to display data.
Currently i have a class, with properties corresponding to the fields, storing them into those properties, and then adding every instance into an ArrayList. Is it possible to access individual properties from the object added to the arraylist, or am i completely in the wrong direction

While objReader.Read
mcp.SurveyQuestion = objReader.GetString(2)
mcp.Option1 = IIf(IsDBNull(objReader.GetValue(3)), " ", objReader.GetString(3))
mcp.Option2 = IIf(IsDBNull(objReader.GetValue(4)), " ", objReader.GetValue(4).ToString)
mcp.Option3 = IIf(IsDBNull(objReader.GetValue(5)), " ", objReader.GetValue(5).ToString)
mcp.Option4 = IIf(IsDBNull(objReader.GetValue(6)), " ", objReader.GetValue(6).ToString)

mcp.Option5 = IIf(IsDBNull(objReader.GetValue(7)), " ", objReader.GetValue(7).ToString)


questionairre.Add(mcp)
End While
where mcp is the instance of the class and questionairre is the Arraylist where i am adding the instance of the object.

Is this a feasible way??
 
GetSurveyQuestions, is the function which connects to the database. I am using the command object there, and that function returns the reader object.
readers are associated to an open db connection. the scope of these objects should be very small. I would argue there is no reason why this object should ever be returned by developer's code.

looking at your logic above the to check for nulls. a cleaner option would be
Code:
string option = string.Concat(row[1], row[2], row[3], row[4]).Trim();

Why pass the row object, and not the values from the row?
because it's the responsiblity of the mapper to parse the items in the row and instaniate a new output object.

I believe one issue that is complicating the matter is the number of objects introduced and how to go about testing them. If your using the M$ <F5> debug method ,then the code i provided can become tedious to walk through.

if your use testing framework (mbunit, nunit) and a mocking framework (Rhino.Mocks) then you can test the individual objects to ensure each piece is working correctly. then create one large intergration test which tests all the pieces together.

i tend to keep my mappers as dumb as possible. all they should do is take values from the input object and map them to the output object.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
because it's the responsiblity of the mapper to parse the items in the row and instaniate a new output object.
There's no reason why the row object has to be passed. It's perfectly acceptable to pass individual parameters to a function that will create the output object.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
Done!!!! Thanks folks. Realised my own folly. Did not instantiate the ArrayList and also was passing the Arraylist the wrong way. Thanks so much. Jason, i am going to give you stars for this. For pointing me in the right direction.

Thanks

Here is the code if anyone else wants to view it.

For Each row As DataRow In table.Rows

mcp = New MultipleChoiceRadio(row.Item(colquestion), IIf(IsDBNull(row.Item(colopt1)), " ", row.Item(colopt1)), IIf(IsDBNull(row.Item(colopt2)), " ", row.Item(colopt2)), IIf(IsDBNull(row.Item(colopt3)), " ", row.Item(colopt3)), IIf(IsDBNull(row.Item(colopt4)), " ", row.Item(colopt4)), IIf(IsDBNull(row.Item(colopt5)), " ", row.Item(colopt5)), row.Item(qId))

qArray.Add(mcp)

' qMapper = New QuestionMapper(mcp)


Next

qMapper = New QuestionMapper(qArray)
 
There's no reason why the row object has to be passed. It's perfectly acceptable to pass individual parameters to a function that will create the output object.
very true.
this does however allow me to use a generic
IMapper<Input, Output> interface. I also prefer to pass a single complex object intead of many simple types.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks to ca8sm too for sorting my VB errors specifically. Stars to everyone who has helped me with this. I am going to tidy up the code a bit. Have made note of the above points. I will post the tidied up code as well, for others to refer.
 
very true.
this does however allow me to use a generic
IMapper<Input, Output> interface. I also prefer to pass a single complex object intead of many simple types.
Yes, that's a good point.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
<IIf(IsDBNull(row.Item(colopt1)), " ", row.Item(colopt1)), [etc]

While I'm more of a VB6 expert than a .Net one, you should be able to accomplish the above more concisely with this: [tt]row.Item(colopt1) & ""[/tt]. The point behind it is that null concatenated to empty string returns empty string, and any other value concatenated to empty string returns said value. Whether you adopt this or not, I would suggest that you replace your spaces with empty strings in any case. As a general rule, the technique of displaying a space when one really doesn't want to display anything at all has a way of coming back to haunt one in unanticipated ways.

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top