5

I need an array and each item in the array is an array of bytes like this, but I'm not sure how to do the:

Dim xx as array

xx(0) *as byte* = {&H12, &HFF}

xx(1) *as byte* = {&H45, &HFE}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
  • 4
    Just a note that this is almost always a bad idea. Byte arrays have their place, but by the time you start thinking about an array of byte arrays you should really think about a _collection_ of byte arrays instead. Use something like a List. – Joel Coehoorn Dec 31 '09 at 22:04

3 Answers3

10

You can make a nested or "jagged" byte array like this:

Dim myBytes(6)() As Byte

This will create an empty array of 6 byte arrays. Each element in the outer array will be Nothing until you assign an array to it, like this:

 myBytes(0) = New Byte() { &H12, &Hff }

However, it would probably be a better idea to make a List of byte arrays, like this:

Dim myBytes As New List(Of Byte())

This will create an empty list of byte array, which will stay empty until you put some byte arrays into it, like this:

myBytes.Add(New Byte() { &H12, &Hff })

Unlike the nested array, a List(Of Byte()) will automatically expand to hold as many byte arrays as you put into it.

For more specific advice, please tell us what you're trying to do.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

Please refer to this MSDN topic for more details.

Here's the code to define a multidimensional array:

Dim lotsaBytes(2,4) As Byte

And to initialize it:

Dim lotsaBytes(,) As Byte = New Byte(2, 4) {{1, 2}, {3, 4}, {5, 6}, {7, 8}}
Eilon
  • 25,582
  • 3
  • 84
  • 102
0

You can solve your problem with the following VB.NET example. Just drag and drop one button and one textbox. The code will be as follows inside the button click event:

Private Sub btnCalcBcc_Click(sender As System.Object, e As System.EventArgs) Handles btnCalcBcc.Click
        Dim BCC As Int16
        Dim Bcc2 As Int16
        Dim arr() As Byte = {&H1B, &H58, &H41, &H42, &H43, &H44, &H45, &H46, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H17, &H0, &H0, &H0, &H0}

        For i As Integer = 0 To arr.Length - 1
            BCC = BCC Xor arr(i)
            BCC = BCC << 1
            Bcc2 = (BCC >> 8)
            Bcc2 = Bcc2 And &H1
            BCC = BCC + Bcc2
        Next
        txtBCC.Text = BCC
End Sub
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
imad
  • 11
  • 3
    It's better to let people give you feedback in public here on Stack Overflow, so everyone can benefit, than privately to you via email. – Peter Hosey May 02 '12 at 07:58