rowmark
rowmark

Reputation:

For each loop in vb.net

How do I use for loop in vb.net something like

dim start as integer
Dim customers as New List(Of Customers)

Customers=dataAcess.GetCustomers()

For each start=500 in  Customers.count
  'Do something here'
Next

I want to process some data for each 500 customers.. Please help

Upvotes: 15

Views: 64605

Answers (8)

Joel Coehoorn
Joel Coehoorn

Reputation: 415665

First of all, don't create a New list of customers if you're just going to assign a different list to the variable on the next line. That's kinda dumb. Do it like this:

Dim customers As List(Of Customer) = dataAccess.GetCustomers()

Then, for the loop you need a plain For loop rather than a For Each. Don't forget to stop before the count of items, since for .Net the first index is 0 instead of 1:

For i As Integer = 500 To Customers.Count -1 
    ' Do something with Customers(i) here
Next i

If you're using Visual Studio 2008 or later you could also write it like this:

For Each item As Customer in  Customers.Skip(500)
   ' Do something with "item" here
Next

Upvotes: 15

JaredPar
JaredPar

Reputation: 754575

Try the following

For Each current In customers
    ' Do something here 
    Console.WriteLine(current.Name)
Next

Upvotes: 13

Fredou
Fredou

Reputation: 20100

You can either try:

    For i As Integer = 500 To (customers.Count -1)
        ''do something
    Next

Or

    For i As Integer = 0 To (customers.Count - 1) step 500
        ''do something
    Next

Upvotes: 0

PowerUser
PowerUser

Reputation: 11791

Obviously, there is no shortage of variety. I don't recognize your "DataAcess" object type, but if you can pull that table in as a Recordset (i.e. a SQL table) then try this. (Don't forget to return the recordset when your done)

Option Explicit

Dim Customers As Recordset
Set Customers=currentdb.openrecordset("???")

While Customers.EOF=False
   'do stuff here using the recordset object
   'i.e. Customers.Fields("Name")="Billy"
   Customers.MoveNext
Wend

Upvotes: 0

Kevin LaBranche
Kevin LaBranche

Reputation: 21078

'This will start at 500 and process to the end....

for start as integer = 500 to Customers.Count

'process customer....
customer = Customers(start)

Next

To iterate the entire list:

for each cust as Customer in Customers

Next 

One note.... VB is case insensitive and your sample code seems to use lower case and upper case customers

Upvotes: 1

Jason
Jason

Reputation: 52523

I'm not exactly sure what you're trying to do, but maybe you could try this:

Dim count As Integer = 0
Const MAX_CUSTOMERS As Integer = 500
Dim customers As List(Of Customers) = dataAcess.GetCustomers()

For Each c As Customer In customers
    'do stuff here'
    c.Name = "Billy"
    count+=1

    If count = MAX_CUSTOMERS Then Exit For
Next

It's not elegant by any means, but it makes sense and it will work.

Upvotes: 0

AnthonyWJones
AnthonyWJones

Reputation: 189437

Something like this:-

Dim customers as New List(Of Customer)

Customers=dataAcess.GetCustomers()

For Each customer AS Customer in  Customers
   '' // do something with the customer object
Next

Edit

Sounds like you want to select 500 of N items or perhaps the next 500. You could use the LINQ extension methods .Take and/or .Skip to achieve this. Use ToList then to create your list. E.g.:-

Dim customers as List(Of Customer)
customers = dataAccess.GetCustomers().Skip(500).Take(500).ToList()

If all you want to do enum through the customers then you could dispense with ToList().

Upvotes: 1

Martin
Martin

Reputation: 2434

Dim start as Integer
Dim customers as New List(Of Customers)

Customers = dataAcess.GetCustomers()

For i as Integer = start to Customers.count Step 500
    Debug.Print Customers(i).someProperty
    Do something here
Next i

I think you need to use the Customer index and Step in 500s. This will only process customer(start), Customer(start+500), Customer(start+1000) etc, not all the customers. Is that what you intend?

Upvotes: 0

Related Questions