473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Passing an array, vb 2005 ee

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).

Jul 5 '07 #1
7 1713
berick <be****@discussions.microsoft.comwrote:
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.)

<snip>
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 5 '07 #2
(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.

"Jon Skeet [C# MVP]" wrote:
berick <be****@discussions.microsoft.comwrote:
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.)

<snip>
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 5 '07 #3
berick <be****@discussions.microsoft.comwrote:
(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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 5 '07 #4
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.

"Jon Skeet [C# MVP]" wrote:
berick <be****@discussions.microsoft.comwrote:
(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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 6 '07 #5
berick <be****@discussions.microsoft.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 6 '07 #6
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.

"Jon Skeet [C# MVP]" wrote:
berick <be****@discussions.microsoft.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 6 '07 #7
berick <be****@discussions.microsoft.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 7 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Oscar | last post by:
I am looking for a way to pass an ADO recordset that has been retrieved in an ASP page to another HTML-page. Is there someone who can provide me with a small sample or a link to see how this is...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
18
by: laclac01 | last post by:
Is there a way to pass a 2d array to a function in c++ with out having to specifiy the number of elements in the array. Here is an example #include<iostream> using namespace std; int...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
6
by: DeepaK K C | last post by:
Could anybody tell me how to pass array to a function by value? -Deepak
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
0
by: =?Utf-8?B?QmlsbHk=?= | last post by:
Hi, VS 2005/.NET 2.0 In my DAL i am running a query and passing the results into a local array. That array is being passed back to the BAL and through to the Presentaltion layer. Now, i would...
13
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper in C# for an SDK that has been supplied as a .LIB file and a .h header file. I have got most of the functions to work but am really struggling with the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.