Solved

ASP.NET loop through array list

Posted on 2009-05-05
7
1,315 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 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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 May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

738 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