473,795 Members | 2,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteLi ne("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 2522
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:#$******** ******@TK2MSFTN GP12.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.WriteLi ne("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.Ope n)
Dim br As New IO.BinaryReader (fs)
Dim abyt() as byte = br.ReadBytes(CI nt(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.Str ingBuilder 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******** *****@TK2MSFTNG P09.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 misunderstandin g 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
15156
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 some way to split the text as its being read in? The file is both tab delimited and has a carriage return at the end of each line. Thanks! using System; using System.IO; using System.Collections;
9
23706
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
1282
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 property. I sub readfile and dim a new object (inside a loop) of the first class and pass the four fields to each property of the first class until the EOF. Within this loop I want to add each object of the first class into the Array list of the...
3
4333
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 Update). Thanks wg class Singleton
0
1263
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, then the datagrid is bound to it, and it works fine. The bottom DataGrid is bound to a different ArrayList that holds the same type as the first ArrayList. Both DataGrids are set to Read-Only. The user selects a row from the top DataGrid, then...
48
4492
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 lot faster with both methods using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below you can find C# test case that should allow you to reproduce this error, to run it you will need to put 2 data files into current directory...
3
3120
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 resulting image back in some other bitmap. It works pretty fast, except for stages of coping pixels between bitmap (object of type Bitmap) and the ArrayList. I'm using the trivial
3
3546
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
842
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 box to show these trees , code works fine again ... i have become MAD in resolving this issue !! create_rndtree(ref al,depth) create_fixtree(ref al,depth); trees are stored in al array of arralist , which is cleared first below is the code...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10436
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.