468,780 Members | 2,243 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,780 developers. It's quick & easy.

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 = DBAccessFuncDecl.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.ASCIIEncoding.ASCII.GetString
(bData, 0, 30)
lval = BitConverter.ToInt64(bData, 31)

Console.WriteLine(lval)

I want to put:

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

but no luck.

Thanks,

Denis

Nov 20 '05 #1
4 36594
Denis,
Dim bData As Byte() = New Byte(size)


Dim bData(size-1) As Byte

or

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

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 20 '05 #2
Cor
Hi Mattias,

Are you sure of this?
Dim bData As Byte() = New Byte(size)

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


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
Nov 20 '05 #3
* "Cor" <no*@non.com> scripsit:
Are you sure of this?
Dim bData As Byte() = New Byte(size)

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


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


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

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #4
Dim bData As New Byte(size-1) {}


Oops, I meant

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

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by simondex | last post: by
8 posts views Thread by intrepid_dw | last post: by
34 posts views Thread by newsposter0123 | last post: by
5 posts views Thread by David Mathog | last post: by
17 posts views Thread by =?Utf-8?B?U2hhcm9u?= | last post: by
13 posts views Thread by WaterWalk | last post: by
6 posts views Thread by Jai Prabhu | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.