Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted

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:

Never trouble another for what you can do for yourself.
Posted
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.
Posted

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.

Never trouble another for what you can do for yourself.
Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...