Solved

ASP.NET loop through array list

Posted on 2009-05-05
7
1,312 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

939 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

9 Experts available now in Live!

Get 1:1 Help Now