Passing an array, vb 2005 ee

G

Guest

New to this, I used to pass an array like this

function BytesToString(byref myarray() as byte, somethingelse as long) as long



and
m = BytesToString(fooBar(), bluenose)

This would send the descriptor or pointer to the array to the function

The acual code is
dim myarray() as byte = My.computer.filesystem.ReadAllBytes(infile)
res = BytesToString(myarray(),1,4)) 'read first 4 bytes only

And the function is as above.

I get a "number of indices is less than the number of dimentions in the
indexed array"

I just do not understand this.. if I put the first element of the array in
there, I get other errors.

This file is one that I have to read Bytes and words from, so I need access
to each and every byte. ALso, the files can be over a gig, so I really do not
want readallbytes, as that can cause problems on some pc's this is to run on,
but fileget isn't working.. which I will post in another question.



(I realy want to use fileget, but that is in another question).
 
J

Jon Skeet [C# MVP]

berick said:
New to this, I used to pass an array like this

function BytesToString(byref myarray() as byte, somethingelse as long) as long

and
m = BytesToString(fooBar(), bluenose)

This would send the descriptor or pointer to the array to the function

The acual code is
dim myarray() as byte = My.computer.filesystem.ReadAllBytes(infile)
res = BytesToString(myarray(),1,4)) 'read first 4 bytes only

I don't think that *is* your actual code:

1) You're passing in three parameters instead of two
2) You've got too many closing brackets

If you could post your real code, I'm sure we can work out what's going
on.

(I'm also somewhat perplexed by the name of your method, given that it
doesn't seem to have anything to do with strings.)

This file is one that I have to read Bytes and words from, so I need access
to each and every byte. ALso, the files can be over a gig, so I really do not
want readallbytes, as that can cause problems on some pc's this is to run on,
but fileget isn't working.. which I will post in another question.

(I realy want to use fileget, but that is in another question).

Any reason for not using a FileStream, which is the idiomatic .NET way
of accessing data from a file?
 
G

Guest

(not sure if this sould be in vs.dotnet.general or here .. my FileGet
question is over there)

Here is actual code:

Dim ress() As Byte = My.Computer.FileSystem.ReadAllBytes(inFile)
res = BytesToString(ress(), 1, 4)
Debug.Print(res)
Debug.Print(UBound(ress).ToString)

Private Function BytesToString(ByRef byteArray() As Byte, _
ByVal Start As ULong, ByVal numchars As Integer) As String

Dim s As String = ""
Dim i As ULong
For i = Start To numchars
s = s & Chr(byteArray(i - 1))

Next
Return s
End Function


Now, there is probably a built in routine to do this, but the help file is
not very robust
to spend much time there, and I tend to search Google or one of the three
books I have for answers, but passing an array I have not found in any manual
or search except on non-oop stuff, and I've been doing non-oop for years.
 
J

Jon Skeet [C# MVP]

berick said:
(not sure if this sould be in vs.dotnet.general or here .. my FileGet
question is over there)

Here is actual code:

Dim ress() As Byte = My.Computer.FileSystem.ReadAllBytes(inFile)
res = BytesToString(ress(), 1, 4)
Debug.Print(res)
Debug.Print(UBound(ress).ToString)

Private Function BytesToString(ByRef byteArray() As Byte, _
ByVal Start As ULong, ByVal numchars As Integer) As String

Dim s As String = ""
Dim i As ULong
For i = Start To numchars
s = s & Chr(byteArray(i - 1))

Next
Return s
End Function

Right:
1) That's a nasty way to build up a string. Use Encoding.GetString,
specifying the appropriate encoding.

2) The reason it wasn't compiling is that the name of the variable is
ress, not ress(). Just change this line:

res = BytesToString(ress(), 1, 4)
to
res = BytesToString(ress, 1, 4)

and it will compile. Encoding.GetString replaces the method completely
though...
Now, there is probably a built in routine to do this, but the help file is
not very robust
to spend much time there, and I tend to search Google or one of the three
books I have for answers, but passing an array I have not found in any manual
or search except on non-oop stuff, and I've been doing non-oop for
years.

Arrays are already reference types. You don't have to pass them *by*
reference.
 
G

Guest

I find that BinaryReader and FileStream.position are doing the job very much
like get and seek did in the past. Very easy. ...

BUT... conversion is not as easy, so far... in the past, I could get a
string of chars from a file that represented a long, in 4 bytes and use CINT
to get the integer it represented. Now I get errors saying the string can't
be converted. I can take the ASC of each item and return the correct value,
but have not found how to get this directly..

The Hex code in the file is exactly this:
12 00 00 00
Where 12 00 is the lsb and 00 00 is the msb

I get this with
r = BinaryReader.readChars(4)

but how do I get the integer? I've tried .readUINT16, and 32 and almost
everything and don't have it. I know it has to be simple, but here is another
item from years of non-oop programming causing a stumble. I can use asc(first
char) & asc(second) and so forth to get it but this can't be what I am
looking for as that can get very messy.
 
J

Jon Skeet [C# MVP]

berick said:
I find that BinaryReader and FileStream.position are doing the job very much
like get and seek did in the past. Very easy. ...

BUT... conversion is not as easy, so far... in the past, I could get a
string of chars from a file that represented a long

Hang on - are you sure you really mean a string of characters? What
*exactly* is the procedure which has been used to convert the data (and
when you say "long" what exactly do you mean?)
in 4 bytes and use CINT
to get the integer it represented. Now I get errors saying the string can't
be converted. I can take the ASC of each item and return the correct value,
but have not found how to get this directly..

The Hex code in the file is exactly this:
12 00 00 00
Where 12 00 is the lsb and 00 00 is the msb

I get this with
r = BinaryReader.readChars(4)

Check *exactly* what ReadChars does - it's probably not exactly what
you're really expecting. Which encoding are you using?
but how do I get the integer? I've tried .readUINT16, and 32 and almost
everything and don't have it. I know it has to be simple, but here is another
item from years of non-oop programming causing a stumble. I can use asc(first
char) & asc(second) and so forth to get it but this can't be what I am
looking for as that can get very messy.

I'd avoid using ASC entirely, and use the Encoding class - that's the
surest way of converting between bytes and characters in an explicit
way.
 
G

Guest

Ok, let me start from scratch. In the previous version, I stored things in a
string, but just needed something to get the following in:

The file is a pure binary with byte-by-byte info and headers which are 2, 4
or 8 bytes. I simply read those into the correct types with get(filenum,num
of bytes) after seeking the correct spot. Then, I get a chunk of data, that
I look at byte by byte and throw some away. Then the result gets put back
with a put statement. I stored all of these in a pure string. NO Unicode...
nothing. Just the plain byte-by-byte stuff. ANd I would repeate until this
ended. It is really raw bytes I need and not a string, but the string was a
convenient container for me to use.

I think maybe a dynamic byte array would work, but I have not used one and
am looking for examples to check out.
 
J

Jon Skeet [C# MVP]

berick said:
Ok, let me start from scratch. In the previous version, I stored things in a
string, but just needed something to get the following in:

The file is a pure binary with byte-by-byte info and headers which are 2, 4
or 8 bytes. I simply read those into the correct types with get(filenum,num
of bytes) after seeking the correct spot. Then, I get a chunk of data, that
I look at byte by byte and throw some away. Then the result gets put back
with a put statement. I stored all of these in a pure string. NO Unicode...
nothing. Just the plain byte-by-byte stuff. ANd I would repeate until this
ended. It is really raw bytes I need and not a string, but the string was a
convenient container for me to use.

Right - string is *not* a convenient way of doing this at all - string
is for text data. BinaryReader is the way forward - just call
ReadInt16, ReadInt32 or ReadInt64.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top