[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

ASP.NET loop through array list

Posted on 2009-05-05
7
Medium Priority
?
1,323 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
Industry Leaders: 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

656 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