Direct Cast Question

  • Thread starter Thread starter pmclinn
  • Start date Start date
P

pmclinn

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()
 
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top