简体   繁体   中英

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

First of all, don't create a New list of customers if you're just going to assign a different list to the reference 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 end of the list:

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

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

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

Try the following

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

'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

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. Eg:-

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().

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
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?

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.

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 (ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM