File reader problem

Eduardo Lorenzo

Well-known member
Joined
Jun 27, 2006
Messages
86
I am making a verification app comparing two text files with each other.

Code:
 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        opd.ShowDialog()
        TextBox2.Text = opd.FileName
        Dim ecreader As New IO.StreamReader(TextBox2.Text)
        f2 = ecreader.ReadLine.ToString
    End Sub

gets the file

Code:
Public Property f2() As String
        Get
            file2 = f2
        End Get
        Set(ByVal Value As String)     <--------- error here
            f2 = Value
        End Set
    End Property

error is Stack Overflow :confused:

did I miss something?
 
Get and Set property accessors

What is the name of your class variable? If it is file2, as the Get accessor seems to indicate, then your property should read:

Code:
    Public Property f2() As String
        Get
            Return file2 Should return a value
        End Get
        Set(ByVal Value As String)
            file2 = Value Should set a value
        End Set
    End Property

The problem with your code was that the Set accessor was calling itself, by trying to set f2 again. Also, your Get accessor did not return anything.

Good luck :cool:
 
MrPaul said:
Also, your Get accessor did not return anything.
Thats actually an old VB way of doing things and perfectly legit from a compilation perspective. Assigning to the name of a function or property is the same as returning a value from that function or property. It should be noted that the assigned value will only be returned once you exit the function or property. In other words, the value wont be returned immediately as it is when you use the return keyword.
 
Wrong assignment

If you take another look at Eduardos code, youll see that his Get accessor does not return anything, either the .Net way or the old VB way. It attempts to set the value of file2, not f2. This would also cause recursion and hence an OutOfStackSpace exception.

Besides, explicitly returning values is a good habit to get into.
 
Back
Top