Eduardo Lorenzo Posted December 12, 2006 Posted December 12, 2006 I am making a verification app comparing two text files with each other. 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 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? Quote
MrPaul Posted December 13, 2006 Posted December 13, 2006 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: 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: Quote Never trouble another for what you can do for yourself.
Eduardo Lorenzo Posted December 13, 2006 Author Posted December 13, 2006 got it.. darn.. I need some coffee.. thank you very much.. Quote
mskeel Posted December 13, 2006 Posted December 13, 2006 Also' date=' your Get accessor did not return anything.[/quote']That's 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 won't be returned immediately as it is when you use the return keyword. Quote
MrPaul Posted December 13, 2006 Posted December 13, 2006 Wrong assignment If you take another look at Eduardo's code, you'll 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. Quote Never trouble another for what you can do for yourself.
mskeel Posted December 13, 2006 Posted December 13, 2006 So it is! Maybe I need some coffee too... Besides, explicitly returning values is a good habit to get into.I agree completely. Another spot of advice...more descriptive method names. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.