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

Direct Cast Question

P: n/a
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
Share this Question
Share on Google+
2 Replies


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.