Guest User

Untitled

a guest
Apr 24th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. # Mastering Arrays
  2.  
  3. Arrays are like lists, or the ability to store multiple values in a single variable. For example, if you wanted to add scores or even just condence your code.
  4.  
  5. ## Visual Basic
  6.  
  7. In visual basic to create an array you must declare traditionaly with "Dim" but after the name you must specify how many items you want in your array (0 included).
  8.  
  9. Dim myName(1) As String
  10.  
  11. MyName(0) = "James"
  12. MyName(1) = "Stone"
  13.  
  14. Dim fullName As String = MyName(0) + ' ' + MyName(1)
  15.  
  16. ## Python
  17.  
  18. Arrays in python are more simple in the fact that you do not have to declare the amount of items in the the list. Arrays are marked by sqaure brackets [] with comma deliminated items. To call items by index (starting at 0) you put the
  19. ```python
  20. listName[0]
  21. ```
  22. for the first item
  23. ```python
  24. listName[1]
  25. ```
  26. and so on
  27. Example:
  28. ```python
  29.  
  30. myName = ["James", "Stone"]
  31. fullName = myName[0] + ' ' + myName[1]
  32.  
  33. ```
  34.  
  35. ### Index Errors
  36.  
  37. These usaly occur when you try and call an item that dose not exist from a list for example:
  38.  
  39. Dim myName(1) As String
  40.  
  41. myName(0) = "James"
  42. myName(1) = "Stone"
  43.  
  44. MessageBox.Show("Your name is: "& Myname(0)& ' ' & myName(1) & ' ' myName(2))
  45.  
  46. This would result in an error because there are only two items in this list.
  47.  
  48. ### Listboxes
  49.  
  50. To add items from arrays to listboxes you can add them individualy or you can use a for loop
  51.  
  52.  
  53. Dim MyNumbers(4) As Integer
  54.  
  55. Dim i As Integer
  56.  
  57. MyNumbers(0) = 10
  58. MyNumbers(1) = 20
  59. MyNumbers(2) = 30
  60. MyNumbers(3) = 40
  61. MyNumbers(4) = 50
  62.  
  63. For i = 0 To 4
  64.  
  65. ListBox1.Items.Add(MyNumbers(i))
  66.  
  67. Next i
  68.  
  69. The next i statement add the end incremens i so the loop only repetes 4 times.
  70.  
  71. ### Assigning values to arrays
  72.  
  73. You can set a variable to parts of an array to be the value of something like a textbox
  74.  
  75. MyName(0) = Val(Textbox1.Text)
  76. MyName(1) = Val(Textbox2.Text)
  77.  
  78. this can make assignation alot easier
  79.  
  80. ### Arrays where the amount of items in not knowen
  81.  
  82. Instead of declaring a number of items you need you can leve the () after the variable name blank and have an undifined amount of items
Add Comment
Please, Sign In to add comment