locked
Arrays to lists - Remind me RRS feed

  • Question

  • User-1668014665 posted

    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
    MyList.Add(W)
    W= Nothing Next n

    First 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 list

    For n as integer = 1 to MyList.count ????

    Next

    OR is it

    For n as integer = 0 to MyList.count - 1 ???

    Next

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

    Wednesday, March 6, 2019 10:39 PM

Answers

  • User-893317190 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 7, 2019 2:02 AM

All replies

  • User475983607 posted

    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

    Wednesday, March 6, 2019 10:56 PM
  • User-1668014665 posted

    I was asking about LISTS, please help out with questions marked ???? in my original posts

    Thursday, March 7, 2019 12:29 AM
  • User-158764254 posted

    zero based

    so:

     MyList.count - 1
    Thursday, March 7, 2019 12:40 AM
  • User-893317190 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 7, 2019 2:02 AM