Solved

ASP.NET loop through array list

Posted on 2009-05-05
7
1,311 Views
Last Modified: 2012-05-06
Hello,

I have a variable of a certain type called Item and one of its properties is called Choices, and that is an object with several properties (like the id, text, dateadded, etc.)

I want to loop through the choices (its an arraylist) in a given item.

Whats the syntax?

Im starting with&

For i = 0 To item.Choices.Count - 1
           
response.write item.Choices(i).dateadded)  this is the line Im having knowing what the syntax is

 Next

Can someone clarify the appropriate syntax to loop through an arraylist property of an object in ASP.NET?

Thanks in advance!

Steve
0
Comment
Question by:skbohler
  • 4
  • 2
7 Comments
 
LVL 12

Accepted Solution

by:
wht1986 earned 500 total points
ID: 24304437
i like to use for each statements generally

Dim choice as MyChoiceObject
Dim dt as DateTime

For Each choice in item.Choices
    dt = choice.dateadded    
Next
0
 
LVL 25

Expert Comment

by:SStory
ID: 24304451
For an arraylist:
        Dim a As New ArrayList
        For I As Integer = 0 To a.Count - 1
            a(I)
        Next

the above is correct.
I think the problem could be that an ArrayList holds objects which aren't necessary the specific type you are looking for.  So you need to cast it probably

If the arraylist held objects of type MyObject
then

        Dim a As New ArrayList
        For I As Integer = 0 To a.Count - 1
            response.write(ctype(a(I),MyObject).MyProperty)
        Next

would help me access it.

If you need to access multiple properties you'd be better to do something like
        Dim a As New ArrayList
        dim MyO as MyObject
        For I As Integer = 0 To a.Count - 1
            MyO=ctype(a(I),MyObject)
            response.write(MyO.Myproperty)
        Next

0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24304456
that looks right if you are using VB.Net for your code behind Which object is the array list? This is how I would do it if the item.Choices is the array list:
For i As Integer = 0 To item.Choices.Count - 1

    Response.Write(item.Choices(i).dateadded)

Next

Open in new window

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Expert Comment

by:wht1986
ID: 24304459
you can do it by index as well if needed

Dim i as Integer
Dim choice as MyChoiceObject
Dim dt as DateTime

For i = 0 to item.Choices.Length-1
    choice = item.Choices(i)
    dt = choice.dateadded
Next
0
 
LVL 25

Expert Comment

by:SStory
ID: 24304471
Looks like someone beat me to it... Well his way is essentially the same.  When possible I like For Each also.
Notice that he too is declaring a variable of type MyChoiceObject so that for each item of Object type it gets cast into the MyChoiceObject datatype so that you can access the properties and methods of that object which is a descendent of Object....of course everything is a descendent of Object in DotNet
0
 
LVL 25

Expert Comment

by:SStory
ID: 24304560
Either way is acceptable... I have found times when an index works better. of course there are some times when a while loop, and a variable and incrementing it myself works better. It depends upon the operation at hand...in your case either looks fine.  For each is simpler.
0
 
LVL 25

Expert Comment

by:SStory
ID: 24304765
I think a split points would have been fair...but that is your choice
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now