none
Creating a one dimentional array in VB 2008

    Question

  • Hi,

    i know it is a silly question, but i have been trying for the last 3 hours to build a simple array, and kept getting errors "nullreference exception was unhalled"

    i want to build a simple one dimentional array that contains 7 numbers (in VB 2008) my code is the following:

     Dim MyArray As ArrayList  ' i tried as Array and didn't work

            MyArray.Item(0) = 1
            MyArray.Item(1) = 2
            MyArray.Item(2) = 3
            MyArray.Item(3) = 4
            MyArray.Item(4) = 5
            MyArray.Item(5) = 6
            MyArray.Item(6) = 7

    i also tried:

    Dim MyArray as Array

    MyArray.setvalue (1,0)

    ect...  no luck

    what am i doing wrong?

    Wednesday, December 18, 2013 6:10 PM

Answers

  • NG,

    For the question you asked, the following is what I mean (keep in mind that in dotNET, it's zero-based):

    Option Strict On Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Dim myArray(6) As Integer myArray(0) = 1 myArray(1) = 2 myArray(2) = 3 myArray(3) = 4 myArray(4) = 5 myArray(5) = 6 myArray(6) = 7 Stop End Sub End Class

    With that said though, explain again what your function is supposed to do? In English, not "code talk".

    You might be better to consider a collection like a List(T) as it's better suited, but since I don't really understand the purpose, I'm premature in suggesting that really.


    Please call me Frank :)

    • Marked as answer by Newbie guy x Wednesday, December 18, 2013 6:35 PM
    Wednesday, December 18, 2013 6:27 PM

All replies

  • Start with this: At the top of the code, put the following:

    Option Strict On
    Option Explicit On

    Don't use an ArrayList - that will trip you up later on as it's not type-safe.

    Declare the array for the type you want:

    Dim myArray(6) As Integer

    ... now try it from there.


    Please call me Frank :)

    Wednesday, December 18, 2013 6:13 PM
  • thanks Frank, but still didn't work, actually what i'm trying to do is a public function that takes an array and returns an array ass follows:

    (i know that a more efficient code can be built :) )

    Public Function CalcSort(ByVal ParamArray args() As Double) As ArrayList Dim a1 As Integer, b1 As Integer, c1 As Integer, d1 As Integer, e1 As Integer, f1 As Integer, g1 As Integer If args.Length <= 0 Then Exit Function For i As Integer = 0 To 6 If args(i) >= a1 Then a1 = args(i) Next i For i As Integer = 0 To 6 If args(i) >= b1 And args(i) <> a1 Then b1 = args(i) Next i For i As Integer = 0 To 6 If args(i) >= c1 And args(i) <> a1 And args(i) <> b1 Then c1 = args(i) Next i For i As Integer = 0 To 6 If args(i) >= d1 And args(i) <> a1 And args(i) <> b1 And args(i) <> c1 Then d1 = args(i) Next i For i As Integer = 0 To 6 If args(i) >= e1 And args(i) <> a1 And args(i) <> b1 And args(i) <> c1 And args(i) <> d1 Then e1 = args(i) Next i For i As Integer = 0 To 6 If args(i) >= f1 And args(i) <> a1 And args(i) <> b1 And args(i) <> c1 And args(i) <> d1 And args(i) <> e1 Then f1 = args(i) Next i For i As Integer = 0 To 6 If args(i) >= g1 And args(i) <> a1 And args(i) <> b1 And args(i) <> c1 And args(i) <> d1 And args(i) <> e1 And args(i) <> f1 Then g1 = args(i) Next i Dim MyArray As ArrayList MyArray.Item(0) = a1 MyArray.Item(1) = b1 MyArray.Item(2) = c1 MyArray.Item(3) = d1 MyArray.Item(4) = e1 MyArray.Item(5) = f1 MyArray.Item(6) = g1 Calcsort = My Array

    End Function


     
    Wednesday, December 18, 2013 6:22 PM
  • NG,

    For the question you asked, the following is what I mean (keep in mind that in dotNET, it's zero-based):

    Option Strict On Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Dim myArray(6) As Integer myArray(0) = 1 myArray(1) = 2 myArray(2) = 3 myArray(3) = 4 myArray(4) = 5 myArray(5) = 6 myArray(6) = 7 Stop End Sub End Class

    With that said though, explain again what your function is supposed to do? In English, not "code talk".

    You might be better to consider a collection like a List(T) as it's better suited, but since I don't really understand the purpose, I'm premature in suggesting that really.


    Please call me Frank :)

    • Marked as answer by Newbie guy x Wednesday, December 18, 2013 6:35 PM
    Wednesday, December 18, 2013 6:27 PM
  • i got part of it. now i can build the array. thanks Frank.

    my code is a function that takes an array of 7 numbers and sorts them to eventually check if any 5 of those 7 numbers are in a straight.

    it is for a poker game i'm trying to do.

    i.e. in 7 cards " 4,8,6,2,2,5,7" i want my function to tell me if there are 5 numbers in a straight. in the above case 4,5,6,7,8

    thanks

     

    Wednesday, December 18, 2013 6:35 PM
  • i got part of it. now i can build the array. thanks Frank.

    my code is a function that takes an array of 7 numbers and sorts them to eventually check if any 5 of those 7 numbers are in a straight.

    it is for a poker game i'm trying to do.

    i.e. in 7 cards " 4,8,6,2,2,5,7" i want my function to tell me if there are 5 numbers in a straight. in the above case 4,5,6,7,8

    thanks

    I'm glad it helped. :)

    You still might want to consider using a List(T) rather than an array. A list is "sort of like" a dynamic array; you can add to and remove from the collection at will and behind the scenes, dotNet will manage the resources for you. A List(T) also has a number of built-in methods which you may can take advantage of.

    For what you're doing, you might also consider doing a search in this forum - there are bunches and bunches of them related to playing cards, dice, all that sort of thing (including one of mine about shuffling a deck of cards I think it was ... I've slept since then but I think that's what it was).

    Anyway, the more you do, the more your brain processes and in time it'll get sorted out. A few years from now you'll be amazed, so keep it up!

    :)


    Please call me Frank :)

    Wednesday, December 18, 2013 6:41 PM
  • You could just use the Array.Sort() method.

    Note, in your original example, you're passing in an array of Doubles, and then comparing them using Integer variables. You need to be careful with this. If you really did pass in doubles with decimal values (i.e. 20.2), your function would not work as you expect it to. Frank's suggestion of adding Option Strict On and Option Explicit On would catch and flag these implicit conversions.


    - HomeGrownCoder My posts are kept as simple as possible for easier understanding. In many cases you can probably optimize or spruce up what I present. Have fun coding!

    Wednesday, December 18, 2013 6:41 PM