Initializing Byte array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Denis C

    #1

    Initializing Byte array

    Hi,

    Newbie here wondering how to initialize a byte array.
    I'm working on the compact framework, and I keep getting
    an error because the byte array is null.

    Here is the code:

    Dim ptr As IntPtr = DBAccessFuncDec l.GetLongVal()
    Dim sval As String
    Dim lval As Long

    Dim size As Integer = 30 + 16

    Dim bData As Byte()

    ***Marshal.Copy (ptr, bData, 0, size)*** HERE IS THE ERROR

    sval = System.Text.ASC IIEncoding.ASCI I.GetString
    (bData, 0, 30)
    lval = BitConverter.To Int64(bData, 31)

    Console.WriteLi ne(lval)

    I want to put:

    Dim bData As Byte() = New Byte(size)

    but no luck.

    Thanks,

    Denis

  • Mattias Sjögren

    #2
    Re: Initializing Byte array

    Denis,
    [color=blue]
    >Dim bData As Byte() = New Byte(size)[/color]

    Dim bData(size-1) As Byte

    or

    Dim bData As New Byte(size-1) {}



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • Cor

      #3
      Re: Initializing Byte array

      Hi Mattias,

      Are you sure of this?
      [color=blue][color=green]
      > >Dim bData As Byte() = New Byte(size)[/color]
      > Dim bData(size-1) As Byte
      > or
      > Dim bData As New Byte(size-1) {}[/color]

      The first says
      A bytearea of x bytes (so x * 0)
      I think that with that is nothing wrong

      The second one says something as
      An area of new empty objects byte with a lenght of x

      I don't think that is good code

      What do I see wrong?

      Cor


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Initializing Byte array

        * "Cor" <non@non.com> scripsit:[color=blue]
        > Are you sure of this?
        >[color=green][color=darkred]
        >>> Dim bData As Byte() = New Byte(size)[/color]
        >> Dim bData(size-1) As Byte
        >> or
        >> Dim bData As New Byte(size-1) {}[/color]
        >
        > The first says
        > A bytearea of x bytes (so x * 0)
        > I think that with that is nothing wrong
        >
        > The second one says something as
        > An area of new empty objects byte with a lenght of x[/color]

        Hmmm... The latter doesn't work at all...

        --
        Herfried K. Wagner [MVP]
        <http://www.mvps.org/dotnet>

        Comment

        • Mattias Sjögren

          #5
          Re: Initializing Byte array

          [color=blue]
          >Dim bData As New Byte(size-1) {}[/color]

          Oops, I meant

          Dim bData() As Byte = New Byte(size-1) {}



          Mattias

          --
          Mattias Sjögren [MVP] mattias @ mvps.org

          Please reply only to the newsgroup.

          Comment

          Working...