473,326 Members | 2,013 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,326 software developers and data experts.

ArrayList, Char, and File ?

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
7 2493
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
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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Christine | last post by:
I have code right now that reads in the file into an arraylist, but it reads in each line of the text file as a single element rather than the separate tab delimited strings in the line. Is there...
9
by: Davids | last post by:
is it true that I cannot dynamically add an item to an array? Eg public char = {"a","b"}; char.Add("newitem"); Do I really have to switch to ArrayList to do this?
4
by: Mike | last post by:
I have declared two classes. The first class has 4 private variables. Each has a property defined. I'm calling a readfile sub from a second class. The second class also has an Arraylist...
3
by: wg | last post by:
I have written a class to contain tags that are loaded from a spreadsheet. I can load the value ok. But from another class I would like to change a value inside the arraylist. Here is my code (see...
0
by: Marcus Kwok | last post by:
I am having a weird problem with my DataGrid that is bound to an ArrayList. My situation is as follows: I have two DataGrids on a modal form. The top grid populates an ArrayList from a file,...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
3
by: Iwanow | last post by:
Hello! My goal is to develop a program that opens a bitmap, copies its pixels to an ArrayList in order to perform some complex calculations (edge detection, Hough transform etc.), and save...
3
by: Martin Arvidsson, Visual Systems AB | last post by:
Hi! I am going crayz, i cant get this to work. and i don't know what the problem is. I have this method public ArrayList ResolveData() { ArrayList workingList = new ArrayList();
0
by: UmeIsmail | last post by:
i am trying to implement GP , and have random initialization in arraylist , the code works fine in debug mode , but when i run it without debugging, all trees come out to be same , if i use message...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.