Results 1 to 11 of 11

Thread: [RESOLVED] file to custom class array, small problem

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2012
    Location
    Belgium
    Posts
    14

    Resolved [RESOLVED] file to custom class array, small problem

    i have made a custom class "lln" (student) and i got a file which contains data which should be read to an lln-array: dblln (student-array)
    it works but if the file contains 3 students, then the array will contain 3 students which are all the same: the last student.
    i really dont see why the 1st student gets replaced by the second when the second is read.

    example file:
    keith
    13 years
    male
    sophie
    16years
    female

    the data will be read correctly but array will look like this:
    sophie (dblln(0).name)
    16years (dblln(0).age)
    female (dblln(0).sex)
    sophie (dblln(1).name)
    16years (dblln(1).age)
    female (dblln(1).sex

    here is the code i use to read the data in dblln (the student-array)
    plln is an integer which indicates the position (you will see)
    Code:
    Dim ifile As System.IO.StreamReader = My.Computer.FileSystem.OpenTextFileReader(_dblln)
                While (Not ifile.EndOfStream)
                    tlln.hallo(ifile.ReadLine, ifile.ReadLine, ifile.ReadLine, ifile.ReadLine, ifile.ReadLine)
                    ReDim Preserve dblln(plln)
                    dblln(plln) = tlln
                    plln += 1
                End While
                ifile.Close()
    i have also tried it reading it into a list(of T) and then converting it to the array but it gave me the same results

  2. #2
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: file to custom class array, small problem

    I could help if I knew what this was:
    "
    vb Code:
    1. tlln.hallo(ifile.ReadLine, ifile.ReadLine, ifile.ReadLine, ifile.ReadLine, ifile.ReadLine)
    "

    What is tlln.hallo?
    This is what separates the men from the boys in here. I really should know what this is but it just looks like an expression to me, with no real value.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2012
    Location
    Belgium
    Posts
    14

    Re: file to custom class array, small problem

    sorry forgot to post, here is the full code for the lln class
    names look strange because i need to do it in dutch

    Code:
    Public Class lln
        Private _naam As String
        Private _klas As String
        Private _bedr As String
        Private _docs As Boolean
        Private _eval As String
    
        Public Sub New()
            _naam = ""
            _klas = ""
            _bedr = ""
            _docs = False
            _eval = ""
        End Sub
    
        Public Sub hallo(ByVal naam, ByVal klas, ByVal bedr, ByVal docs, ByVal eval)
            _naam = naam
            _klas = klas
            _bedr = bedr
            _docs = docs
            _eval = eval
        End Sub
    
        Public Property Naam() As String
            Get
                Return _naam
            End Get
            Set(ByVal Value As String)
                _naam = Value
            End Set
        End Property
    
        Public Property Klas() As String
            Get
                Return _klas
            End Get
            Set(ByVal Value As String)
                _klas = Value
            End Set
        End Property
    
        Public Property Bedrijf() As String
            Get
                Return _bedr
            End Get
            Set(ByVal Value As String)
                _bedr = Value
            End Set
        End Property
    
        Public Property Documenten() As Boolean
            Get
                Return _docs
            End Get
            Set(ByVal Value As Boolean)
                _docs = Value
            End Set
        End Property
    
        Public Property Evaluatie() As String
            Get
                Return _eval
            End Get
            Set(ByVal Value As String)
                _eval = Value
            End Set
        End Property
    End Class

  4. #4
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: file to custom class array, small problem

    Ok so 5 properties line up with all the Readline methods being returned. Gotchya.

    Let me think about this a moment first...
    I'll BBL

  5. #5
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: file to custom class array, small problem

    I think what we see in your Writing methods we could better determine why your code is causing this mess.

  6. #6
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: file to custom class array, small problem

    Should it not be Public Class tlln instead of what you have Public Class lln?
    tlln.hallo()

    or have you declared tlln As New lln somewhere in code?

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2012
    Location
    Belgium
    Posts
    14

    Re: file to custom class array, small problem

    ooh another thing i forgot. no, its correct like that, here is tlln
    Public tlln As lln = New lln

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2012
    Location
    Belgium
    Posts
    14

    Re: file to custom class array, small problem

    you can download full project here if u wish
    https://docs.google.com/open?id=0BzM...kxmTmcwX0FoeEk

  9. #9
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: file to custom class array, small problem

    Just create a New instance of that class and it works fine

    vb Code:
    1. If System.IO.File.Exists(_dblln) = True Then
    2. Dim ifile As System.IO.StreamReader = My.Computer.FileSystem.OpenTextFileReader(_dblln)
    3.  
    4.             While (Not ifile.EndOfStream)
    5.                 tlln = New lln ''--<< right here create new instance tlln --\\
    6.                 tlln.hallo(ifile.ReadLine, ifile.ReadLine, ifile.ReadLine, ifile.ReadLine, ifile.ReadLine)
    7.  
    8.                 ReDim Preserve dblln(plln)
    9.                 dblln(plln) = tlln
    10.  
    11.                 plln += 1
    12.             End While
    13.             ifile.Close()
    14.  
    15.         Else
    16.             dblln(0) = tlln
    17.         End If

  10. #10

    Thread Starter
    New Member
    Join Date
    May 2012
    Location
    Belgium
    Posts
    14

    Re: file to custom class array, small problem

    what a stupid thing to miss.
    THANKS SO MUCH FOR HELPING ME

  11. #11
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: file to custom class array, small problem

    Quote Originally Posted by arijspieter View Post
    what a stupid thing to miss.
    THANKS SO MUCH FOR HELPING ME
    I don't know how these Ratings work, but could you please rate my post to resolve, and then Enter Resolved for this thread -from your control panel.

    It makes me feel good when I can help.
    It is a reason I do come in and help when I can.
    There are not many people these days that express gratitude, but you will find programmers are among the most gratuitous people on the planet.

    So says I

    I see you resolved and did what I asked.
    Thank you so much too

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width