ArrayList, Char, and File ?

M

millenium

Several related questions:

1. Can an ArrayList hold a Byte or a Char ?

why is this not working

Dim al As ArrayList
Dim ch As Char
ch = "c"
al.Add(ch)


2. Is the following code for reading in a binary file considered a good
way of doing it, it does work, but a little slowly.

FileOpen(2, "file.bin", OpenMode.Binary, OpenAccess.Read)
Dim uBound As Integer = 10000
Dim rx(uBound) As Byte
Dim ch As Byte
Dim i As Integer = 0
Console.WriteLine("please wait while file is loading")
While Not EOF(2)
FileGet(2, ch)
rx(i) = ch
If i = uBound Then
uBound += 10000
ReDim Preserve rx(uBound)
Console.Write(".")
End If
i += 1
End While


3. Lastly, if I only need to read a byte from the file at random location x,
do I need to read the file into an array as I have done above, or is there
a way to simply access one byte?

Thanks.
 
P

Peter Proost

1. doesn't work because it should be:
Dim al As NEW ArrayList
Dim ch As Char
ch = "c"
al.Add(ch)

don't forget the NEW with an arraylist

for the moment I haven't got time to check out your second question because
I'm at work

hth Peter
 
C

Cor Ligthert

Millenium,

First of all you can set on
Option Strict On in top of your program, than your program will be probably
faster
With Option Strict
VBNet = C#
Without Option Strict
VBNet = VB6
And a lot of runtime errors are showed in advance

Original code deleted.
\\\
Dim al As New ArrayList
Dim ch As Char
ch = "c"c
al.Add(ch)
///
2. Is the following code for reading in a binary file considered a good
way of doing it, it does work, but a little slowly.

Original Code deleted in this message
\\\
Dim fs As New IO.FileStream("FilePath", _
IO.FileMode.Open)
Dim br As New IO.BinaryReader(fs)
Dim abyt() as byte = br.ReadBytes(CInt(fs.Length))
br.Close()
///
3. Lastly, if I only need to read a byte from the file at random location
x,
do I need to read the file into an array as I have done above, or is there
a way to simply access one byte?
See for that this page
http://msdn.microsoft.com/library/d...ef/html/frlrfsystemiofilestreamclasstopic.asp

I hope this helps,

Cor
 
M

millenium

Thanks all, lastly, regarding reading of a file one char at a time,
although I shall use Cor's way, I still would like to ask
what would be considered better, to use an ArrayList
which grows automatically, or to use a redim ststement
every , say 10000, or whatever. ArrayList seems to be
quicker to code because there is no 'if' statement, so it
it o.k. to do it this way. The file size, b.t.w., would be
several Mb.

Tia.
 
C

Cor Ligthert

Millenium

That is quit simple to answer use Redim only when it is in your upgraded VB
classic application and change it as fast to the arraylist as possible.

An arraylist is only a collection of objects which you can add and delete as
much as you want, a redim redimensions the array and should make a new one
for that.

I hope I don't have to explain more?

Cor
 
J

Jay B. Harlow [MVP - Outlook]

Millenium,
Are you putting 10000 or more Chars into an ArrayList?

One at a time?

I would seriously consider not doing that as you are going to box all 10000
characters which can put increased pressure on the Garbage collector.

Depending on what I am using the Chars for I would probably use a
System.Text.StringBuilder instead as it allows you to dynamically grow the
"buffer" holding the chars, plus there is no boxing of Chars going on.

Alternatively you could encapsulate your Char array into a new class
CharArrayList that does the Redim Preserve on an internal array of Char
expanding it as needed. This way the logic to handle the array is separate
from the logic to read the file...

Going back to your original question if you use a FileStream as Cor showed,
you can use FileStream.Seek to position to a specific spot & read from that
position. You should be able to position the Stream then use the
BinaryReader to read the Char.

Hope this helps
Jay
 
C

Cor Ligthert

Jay,

As seldom I did answer the first question more about the error.
(It was such a nice solution I made for that Dennis D.)

I completly agree with you about the stringbuilder.
(We did not see that favorite of us a while).

Only to show Millenium that we have no misunderstanding about that.

Cor
 

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