We are excited to announce that the ASP.NET Forums are moving to the new Microsoft Q&A experience. Learn more >

# Arrays to lists - Remind me[Answered] RSS

## 4 replies

Last post Mar 07, 2019 02:02 AM by Ackerly Xu

• icm63

Member

249 Points

1064 Posts

### Arrays to lists - Remind me

Mar 06, 2019 10:39 PM|icm63|LINK

The array understanding

```Structure SArrayTest
Public Num1 as integer
Public Num2 as integer
End Structure

Dim MyArr(10) as SArrayTest

'Array index is 0 to 9, Zero is first
For n as integer = 0 to MyArr.getUpperbound(0)
MyArr(n).Num1 = n + 1
MyArr(n).Num2 = n + 2
Next n

First Item is : MyArr(0)
Second Item is : MyArr(1)
Get Last item in array: MyArr(MyArr.getUpperbound(0))
Count size of array is: MyArr.Length

```

The list understanding

```Public Class SListTest
Public Property Num1 as integer
Public Property Num2 as integer
End Class

Dim MyList AS List(Of SListTest)

'OK NOW PLEASE REMIND ME

'Is list index is 1 first or ZERO
For n as integer = 0 to 10   Dim w as new SListTest
W.Num1 = n + 1   W.Num2 = n + 2
Next nFirst item is: MyList(0) or (1)???Second item is: MyList(1) or (2)???Get last item in List:  MyList(Mylist.Count-1) ????Count size of MyList: MyList.count ??? Determine the number of items in a list: MyList.count -1 or MyList.count ???Looping through a listFor n as integer = 1 to MyList.count  ????NextOR is itFor n as integer = 0 to  MyList.count - 1 ???Next```

Question could you please answer the  ??? about how to handle lists as compared to arrays

icm63
• mgebhard

All-Star

53711 Points

24042 Posts

### Re: Arrays to lists - Remind me

Mar 06, 2019 10:56 PM|mgebhard|LINK

By default .NET array and List types are zero based.  IIRC, VB.NET arrays or collections can be configured as zero or one based for backward compatibility.  I'm pretty sure List(Of ) is zero based all the time... The programming docs cover the basics but I would stick with zero based if given a choice.

https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/arrays/

https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/strings/zero-based-vs-one-based-string-access

• icm63

Member

249 Points

1064 Posts

### Re: Arrays to lists - Remind me

Mar 07, 2019 12:29 AM|icm63|LINK

icm63
• mbanavige

All-Star

160090 Points

13198 Posts

ASPInsiders

Moderator

### Re: Arrays to lists - Remind me

Mar 07, 2019 12:40 AM|mbanavige|LINK

zero based

so:

` MyList.count - 1`
Mike Banavige
• Ackerly Xu

Contributor

3500 Points

1300 Posts

### Re: Arrays to lists - Remind me

Mar 07, 2019 02:02 AM|Ackerly Xu|LINK

Hi icm63,

As mgebhard and mbanavige have all mentioned, list is zero-based,so  the answer to your question is

First item Is: MyList(0) Or (1)??? ' because List's index starts with zero, first item is MyList(0)
Second item Is: MyList(1) Or (2)??? ' then second itme is MyList(1)
Get last item In List: MyList(Mylist.Count-1) ???? ' the last item in MyList is MyList(MyList.Count-1)
Count size Of MyList: MyList.count ??? ' MyList.Count always returns the count of the element is the list, so in your case it is 11
Determine the number Of items In a list: MyList.count -1 Or MyList.count ??? duplicate with the previous question, its MyList.Count

To loop the list , foreach loop is usually used instead of for loop , using foreach loop , you don't need to get the count of the list.

``` ' for loop       For index = 0 To MyList.Count - 1
Response.Write((MyList(index).Num1 + MyList(index).Num2) & "<br/>")
Next ' foreach loop
For Each item In MyList
Response.Write((item.Num1 + item.Num2) & "<br/>")
Next```

Best regards,

Ackerly Xu

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.