?
Solved

ASP.NET loop through array list

Posted on 2009-05-05
7
Medium Priority
?
1,318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 12

Accepted Solution

by:
wht1986 earned 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

762 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