For each cycle in vb.net

How to use something like for a loop in vb.net

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 every 500 clients. Please, help

+7
source share
8 answers

First of all, do not create Newa customer list if you are just going to designate another list for the link in the next line. This is a little stupid. Do it like this:

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

Then for the loop, you need a simple "For" loop, not a for each. Remember to stop to the end of the list:

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

If you are using Visual Studio 2008, you can also write it like this:

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

Try to execute

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

' 500 .

for start as integer = 500 to Customers.Count

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

Next

:

for each cust as Customer in Customers

Next 

.... VB , , ,

+1

- : -

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

, 500 N , , 500. , LINQ .Take / .Skip. ToList, . : -.

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

, ToList().

+1

:

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

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

, Customer Step in 500s. (), ( + 500), ( + 1000) .., . , ?

0

, , , , :

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

, , .

0

Obviously, there is no lack of diversity. I do not recognize your type of DataAcess object, but if you can pull this table as a set of records (i.e. SQL tables), try this. (Remember to return the recordset at runtime)

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
0
source

Source: https://habr.com/ru/post/1716004/


All Articles