2 Replies - 668 Views - Last Post: 15 April 2012 - 04:53 PM Rate Topic: -----

#1 Jas0791  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-September 11

Unable to open file and fill collection with file contents.

Posted 15 April 2012 - 09:06 AM

I have a Motor Class, along with a Collection to hold objects address. Each Collection object holds the Class fields as a String to display in list box. I am able to write the collection string to the file and save the string. But unable to open the existing file, read the file back into the collection object.

Public Class Motor

    Private strId As String
    Private strDescription As String
    Private dblRpm As Double
    Private dblVoltage As Double
    Private strStatus As String
 'Overridden ToString method.
    Public Overrides Function ToString() As String
        Dim str As String
        str = "Motor ID: " & Id & "," & vbTab & "Type: " & Description & "," & vbTab &
              dblRpm.ToString() & " RPM's," & vbTab & dblVoltage.ToString() & " volts, " & vbTab & " Status: " & Status
        Return str
    End Function
End Class
Module MotorModule
    'g_motorCollection holds Motor objects.
    Public g_motorCollection As Collection

End Module
'Main Form
Imports System.IO

Public Class MainForm

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

        'Create the collection object.
        g_motorCollection = New Collection()
        'Open existing file. (Motor.txt)
        OpenFile()

    End Sub

    Private Sub MainForm_Activated(sender As Object, e As System.EventArgs) Handles Me.Activated
        Dim i As Integer
        Dim temp As Motor

        'Clear the list box.
        lstMotors.Items.Clear()
        'Add the items in the collection to the list box.
        For i = 1 To g_motorCollection.Count
            temp = CType(g_motorCollection.Item(i), Motor)
            lstMotors.Items.Add(temp.ToString())        'Calls the overridden ToString method.
        Next
    End Sub

    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
        Dim frmAdd As New AddForm
        frmAdd.ShowDialog()
        'UpdateListBox()
    End Sub

    Sub OpenFile()
        Dim motorFile As StreamReader
        Dim strRead As String

        Try
            motorFile = File.OpenText("Motor.txt")
            Do Until motorFile.Peek = -1
                strRead = motorFile.ReadLine()
                g_motorCollection.Add(strRead)
            Loop
            'Close the file.
            motorFile.Close()
        Catch ex As Exception
            MessageBox.Show("File can not be opened.")
        End Try
    End Sub
    'Private Sub UpdateListBox()

    'Dim i As Integer
    'Dim str As String = ""

    '   lstMotors.Items.Clear()

    '    For i = 1 To g_motorCollection.Count
    '       str = "Motor ID:  " & motors(i).Id & ", " & motors(i).Description & ", " & _
    '          motors(i).RPM & " RPM's, " & motors(i).Voltage & " volts, Status:  " & motors(i).Status
    '     lstMotors.Items.Add(str)
    'Next

    'End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        'Close the form.
        Me.Close()
    End Sub

End Class




I have looked all over for anything (samples or tutorials for working with files and collection objects) pertaining to type of operation with out any success. Tips with explanation as to how this works would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Unable to open file and fill collection with file contents.

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,358
  • Joined: 02-June 10

Re: Unable to open file and fill collection with file contents.

Posted 15 April 2012 - 09:19 AM

You're trying to re-invent the wheel here. There is no need to micro-manage the read/write process. Just serialize the object.

Separating data from GUI - PLUS - serializing the data to XML
Was This Post Helpful? 0
  • +
  • -

#3 Jas0791  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-September 11

Re: Unable to open file and fill collection with file contents.

Posted 15 April 2012 - 04:53 PM

View PosttlhIn`toq, on 15 April 2012 - 04:19 PM, said:

You're trying to re-invent the wheel here. There is no need to micro-manage the read/write process. Just serialize the object.

Separating data from GUI - PLUS - serializing the data to XML


I read your reply, thanks for responding, although I'm not sure as to what you are meaning.
First did I not use the correct posting location?
Where is the help section located from the main page?
Second, what do I include in the code section of the posting?
The entire program or just the parts that I am having issues with?
I don't come here that much, only when I have run out of options...
I understand everyone is busy with their own thing, so I am still learning the site
itself and how to post for assistance with the issues. Yes I am a student, but I am willing to
work for my stuff, its just that talking to my instructor is like talking to a wall sometimes.
I am left more confused afterward and he is scratching his head himself at times. His instructions
to us are that we need to talk to others and learn from them, that is how we learned of the site in
the first place.
One last thing, I noticed that you do C primarily, so does that reference option work with VB also?

Thanks for your time.
Jeff
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1