16

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

3
  • Do you mean you want the loop to iterate on the List of Customers in chunks of 500? Commented Aug 25, 2009 at 15:20
  • 2
    Could you clarify what you mean by "each 500 customers"? Commented Aug 25, 2009 at 15:21
  • 2
    don't create a "new" list of customers if you're just going to replace it on the next line. Commented Aug 25, 2009 at 15:30

8 Answers 8

16

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
Sign up to request clarification or add additional context in comments.

Comments

13

Try the following

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

Comments

1

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

2 Comments

I want to do it for each 500 customers.
Do you mean you want to do it for every 500th customer, perhaps? It is still unclear what you are trying to accomplish.
1

'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

1 Comment

When I try this for start as integer = 500 to Customer.Count I am getting an error saying customer cannot be converted to int
0
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?

Comments

0

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.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.