By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,336 Members | 2,315 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,336 IT Pros & Developers. It's quick & easy.

ArrayList, Char, and File ?

P: n/a
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.

Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

"millenium" <o:)> wrote in message
news:#$**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 21 '05 #2

P: n/a
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/de...classtopic.asp

I hope this helps,

Cor
Nov 21 '05 #3

P: n/a
"Peter Proost" <pp*****@nospam.hotmail.com> schrieb:
Dim ch As Char
ch = "c"


.... 'ch = "c"c'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #4

P: n/a
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.
Nov 21 '05 #5

P: n/a
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
Nov 21 '05 #6

P: n/a
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

"millenium" <o:)> wrote in message
news:ud*************@TK2MSFTNGP09.phx.gbl...
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.

Nov 21 '05 #7

P: n/a
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
Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.