473,386 Members | 1,791 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,386 software developers and data experts.

Direct Cast Question

I have another question about directcast:

The code below splits a comma del. file into arrays for processing.
How would I directly cast the first value of the first array in this
arraylist:

directcast(aCollection, ....

Dim aCollection As New Collection
Dim aFileStream As New System.IO.FileStream("TestFile.txt",
IO.FileMode.Open)
Dim aStreamReader As New System.IO.StreamReader(aFileStream)
Dim strRecord As String
strRecord = aStreamReader.ReadLine
Do Until strRecord Is Nothing
Dim anArray As String() = strRecord.Split(Chr(Asc(",")))
aCollection.Add(anArray)
strRecord = aStreamReader.ReadLine
Loop
aFileStream.Close()
aStreamReader.Close()

Nov 21 '05 #1
2 2260
pmClinn,

Why do you use that collection, try to avoid that, it is one of the classic
VB parts you better cannot use because all other collections and arrays have
zero indexers and this one with is one indexer makes it useless complex.

So not tested it will be become something as
\\\
Dim anArrayList As New ArrayList
Dim aFileStream As New System.IO.FileStream("TestFile.txt",
IO.FileMode.Open)
Dim aStreamReader As New System.IO.StreamReader(aFileStream)
Dim strRecord As String
strRecord = aStreamReader.ReadLine
Do Until strRecord Is Nothing
Dim anArray As String() = strRecord.Split(","c)
anArrayList.Add(anArray)
strRecord = aStreamReader.ReadLine
Loop
aFileStream.Close()
aStreamReader.Close()
///
You can than do to get the first item
\\\
dim myfirstitem as string = directcast(anArrayList(0), String())(0)
///
I hope this helps?

Cor

"pmclinn" <pe***@mclinn.com>
I have another question about directcast:

The code below splits a comma del. file into arrays for processing.
How would I directly cast the first value of the first array in this
arraylist:

directcast(aCollection, ....

Dim aCollection As New Collection
Dim aFileStream As New System.IO.FileStream("TestFile.txt",
IO.FileMode.Open)
Dim aStreamReader As New System.IO.StreamReader(aFileStream)
Dim strRecord As String
strRecord = aStreamReader.ReadLine
Do Until strRecord Is Nothing
Dim anArray As String() = strRecord.Split(Chr(Asc(",")))
aCollection.Add(anArray)
strRecord = aStreamReader.ReadLine
Loop
aFileStream.Close()
aStreamReader.Close()

Nov 21 '05 #2
In addition to Cor comments, you may want to make your own collection.
Depends what else you need to do with the data:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim aCollection As New LineItemsCollection
Dim aFileStream As New System.IO.FileStream("testfile.txt",
IO.FileMode.Open)
Dim aStreamReader As New System.IO.StreamReader(aFileStream)
Dim strRecord As String
strRecord = aStreamReader.ReadLine
Do Until strRecord Is Nothing
Dim anArray As String() = strRecord.Split(Chr(Asc(",")))
Dim myLineItem As LineItem
With myLineItem
.FirstItem = anArray(0)
.SecondItem = anArray(1)
.ThirdItem = anArray(2)
End With
aCollection.Add(myLineItem)

strRecord = aStreamReader.ReadLine
Loop
aFileStream.Close()
aStreamReader.Close()

Dim x As LineItem = aCollection.Item(0)
Dim myvalue As String = x.FirstItem
End Sub

Public Structure LineItem
Dim FirstItem As String
Dim SecondItem As String
Dim ThirdItem As String
End Structure

Public Class LineItemsCollection
Inherits System.Collections.CollectionBase

Public Sub Add(ByVal aLineItem As LineItem)
List.Add(aLineItem)
End Sub

Public ReadOnly Property Item(ByVal index As Integer) As LineItem
Get
Return CType(List.Item(index), LineItem)
End Get
End Property

End Class

Greg

"Cor Ligthert" <no************@planet.nl> wrote in message
news:uS**************@TK2MSFTNGP10.phx.gbl...
pmClinn,

Why do you use that collection, try to avoid that, it is one of the
classic VB parts you better cannot use because all other collections and
arrays have zero indexers and this one with is one indexer makes it
useless complex.

So not tested it will be become something as
\\\
Dim anArrayList As New ArrayList
Dim aFileStream As New System.IO.FileStream("TestFile.txt",
IO.FileMode.Open)
Dim aStreamReader As New System.IO.StreamReader(aFileStream)
Dim strRecord As String
strRecord = aStreamReader.ReadLine
Do Until strRecord Is Nothing
Dim anArray As String() = strRecord.Split(","c)
anArrayList.Add(anArray)
strRecord = aStreamReader.ReadLine
Loop
aFileStream.Close()
aStreamReader.Close()
///
You can than do to get the first item
\\\
dim myfirstitem as string = directcast(anArrayList(0), String())(0)
///
I hope this helps?

Cor

"pmclinn" <pe***@mclinn.com>
I have another question about directcast:

The code below splits a comma del. file into arrays for processing.
How would I directly cast the first value of the first array in this
arraylist:

directcast(aCollection, ....

Dim aCollection As New Collection
Dim aFileStream As New System.IO.FileStream("TestFile.txt",
IO.FileMode.Open)
Dim aStreamReader As New System.IO.StreamReader(aFileStream)
Dim strRecord As String
strRecord = aStreamReader.ReadLine
Do Until strRecord Is Nothing
Dim anArray As String() = strRecord.Split(Chr(Asc(",")))
aCollection.Add(anArray)
strRecord = aStreamReader.ReadLine
Loop
aFileStream.Close()
aStreamReader.Close()


Nov 21 '05 #3

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

Similar topics

4
by: Richard Lee | last post by:
Hi, I have a question when I do a data type cast. the common way when we do a cast, is we know the type we want to cast to, i.e. we want to cast object to string, object xyz = "question";...
15
by: Christopher Benson-Manica | last post by:
If you had an unsigned int that needed to be cast to a const myClass*, would you use const myClass* a=reinterpret_cast<const myClass*>(my_val); or const myClass* a=(const myClass*)myVal; ...
5
by: MC | last post by:
Hi If I have a pointer to a some structure say for example payroll_ptr where struct payroll { ... } has some members and if i use a function argument as int function_process ( (payroll_ptr)...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
1
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
1
by: Mike | last post by:
Hi We are new to the world of ASP Development and I have a simple question - we are starting a test development in ASP 2.0 Beta and we are building an application using a Direct Connection to...
4
by: dba_222 | last post by:
Dear Experts, Ok, I hate to ask such a seemingly dumb question, but I've already spent far too much time on this. More that I would care to admit. In Sql server, how do I simply change a...
5
by: Remco van Engelen | last post by:
Hello, I have a question regarding the ISO C grammar. The syntax of a direct-declarator reads (section A.2.2, page 413 in my copy; the (R1) is just to 'name' the rule for later reference): ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.