views:

810

answers:

8

Hello, 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

+6  A: 

Try the following

For Each current In customers
  '' // Do something here 
  Console.WriteLine(current.Name)
Next
JaredPar
+1  A: 

'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

klabranche
When I try this for start as integer = 500 to Customer.CountI am getting an error saying customer cannot be converted to int
You have to set your customer to the customers(start) index...
klabranche
A: 

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

AnthonyWJones
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.
JohnFx
A: 
    For i As Integer = 500 To (customers.Count -1)
        ''do something
    Next

or do you want

    For i As Integer = 0 To (customers.Count - 1) step 500
        ''do something
    Next
Fredou
A: 
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?

Martin
A: 

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 here
Next
Joel Coehoorn
A: 

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.

Jason
A: 

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
PowerUser