Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / March 2008

Tip: Looking for answers? Try searching our database.

Get Property also runs Set?!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alan Gillott - 07 Mar 2008 21:20 GMT
While line by line debugging some code I noticed that whenever I run through
Property Set it also subsequently calls Property Set with the having been
Gotten data? Can anyone explain.
A
Alan Gillott - 07 Mar 2008 21:33 GMT
AAAAAARRRGGGHHHH! See correction below:
> While line by line debugging some code I noticed that whenever I run through
> Property GET it also subsequently calls Property Set with the having been
> Gotten data? Can anyone explain.
> A
Herfried K. Wagner [MVP] - 07 Mar 2008 21:42 GMT
"Alan Gillott" <argillott.x@gmail.com> schrieb:
> While line by line debugging some code I noticed that whenever I run
> through Property Set it also subsequently calls Property Set with the
> having been Gotten data? Can anyone explain.

Post your implementation of the property.

Maybe you are recursively calling the 'Set' accessor of the property?

\\\
Public Property Foo() As Object
   Set(ByVal Value As Object)
       Me.Foo = Vaue
   End Set
   ...
End Property
///

This won't work.  Use the code below instead:

\\\
Private m_Foo As Object

Public Property Foo() As Object
   Set(ByVal Value As Object)
       m_Foo = Vaue
   End Set
   ...
End Property
///

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>

kimiraikkonen - 07 Mar 2008 21:53 GMT
On Mar 7, 11:42 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.at> wrote:
> "Alan Gillott" <argillot...@gmail.com> schrieb:
>
[quoted text clipped - 32 lines]
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>

Correction against mistype: Change "vaue"s to "value" in Herfried's
post.
Stephany Young - 08 Mar 2008 03:17 GMT
If that's the most significant contribution you have to a thread then don't
bother.

It was painfully obvious to anyone with half a brain exactly what Herfried
was demonstrating.

On Mar 7, 11:42 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.at> wrote:
> "Alan Gillott" <argillot...@gmail.com> schrieb:
>
[quoted text clipped - 32 lines]
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Correction against mistype: Change "vaue"s to "value" in Herfried's
post.
kimiraikkonen - 08 Mar 2008 09:34 GMT
> If that's the most significant contribution you have to a thread then don't
> bother.
[quoted text clipped - 46 lines]
>
> - Show quoted text -

I just wanted to pay attention for OP not to get an annoying error.
Saying this of you is completely nonsense and against goodwill that i
just wanted to warned the OP with the purpose of being complementary,
nothing more.
Stephany Young - 08 Mar 2008 10:23 GMT
That's just sanctimonious claptrap.

As the OP is quite capable of single-stepping through his code, I am
absolutely certain that he is quite capable of resolving a simple typo
without someone attempting to save him from himself.

Such posts do nothing more than clutter up threads, and the newsgroup in
general, and add no value to the discussion at hand whatsoever.

On Mar 8, 5:17 am, "Stephany Young" <noone@localhost> wrote:
> If that's the most significant contribution you have to a thread then
> don't
[quoted text clipped - 50 lines]
>
> - Show quoted text -

I just wanted to pay attention for OP not to get an annoying error.
Saying this of you is completely nonsense and against goodwill that i
just wanted to warned the OP with the purpose of being complementary,
nothing more.
Galen Somerville - 08 Mar 2008 15:59 GMT
> That's just sanctimonious claptrap.
>
[quoted text clipped - 66 lines]
> just wanted to warned the OP with the purpose of being complementary,
> nothing more.
Sort of like top poster's?

Galen
Steve Gerrard - 08 Mar 2008 16:51 GMT
> That's just sanctimonious claptrap.
>
> Such posts do nothing more than clutter up threads, and the newsgroup
> in general, and add no value to the discussion at hand whatsoever.

And your post is somehow adding value? Talk about pots calling kettles black...
Alan Gillott - 08 Mar 2008 17:53 GMT
Code;

Public Property Title() As String
       Get
           Return c_Title
       End Get
       Set(ByVal value As String)
           c_Title = value
       End Set
   End Property
End Class

Seems simple enough to me!
A

> "Alan Gillott" <argillott.x@gmail.com> schrieb:
>> While line by line debugging some code I noticed that whenever I run
[quoted text clipped - 26 lines]
> End Property
> ///
Cor Ligthert[MVP] - 09 Mar 2008 13:11 GMT
Alan,

Did you write somewhere in that class?

c_Title = Title

Cor

> Code;
>
[quoted text clipped - 41 lines]
>> End Property
>> ///
Alan Gillott - 09 Mar 2008 15:24 GMT
No. That was my first error in my first ever .net class: changing title to
value actually made the class work properly.

> Alan,
>
[quoted text clipped - 49 lines]
>>> End Property
>>> ///
(O)enone - 08 Mar 2008 11:38 GMT
> While line by line debugging some code I noticed that whenever I run
> through Property Get it also subsequently calls Property Set with the
> having been Gotten data? Can anyone explain.

Are you by any chance passing calling Property Get as part of a call into a
procedure, passing it in as a ByRef parameter?

If so, when the procedure finishes executing, it transfers the value back to
the property (because you passed it by reference, not by value).

For example, take the following code:

\\\
   Private Sub MySub
       DoSomething(MyProperty)
   End Sub

   Public Sub DoSomething(ByRef value As String)
       value = "new value"
   End Sub

   Public Property MyProperty() As String
       Get
           Debug.Print("Get")
           Return "return value"
       End Get
       Set(ByVal value As String)
           Debug.Print("Set: " & value)
       End Set
   End Property
///

If you call MySub, it'll call MyProperty Get to retrieve the string "return
value". This is then passed By Reference into DoSomething, which changes the
string to "new value". When DoSomething finishes, VB automatically and
implicitly calls MyProperty Set in order for the updated value to be passed
back into the property. You'll see that the "value" variable in the Property
Set now contains the string "new value".

This has confused me a couple of times in the past -- there's no obvious
programmatic reason why the Property Set gets called. But that's why it is.

Does that explain it? Or are you seeing something else?

If this is it, you can stop either stop it by passing the parameter By Value
(which is always the best thing to do unless you really do want
modifications to the value within the called procedure to be changed in the
variable in the calling procedure too), or just accept that this is what it
needs to do in order for the variable in the calling procedure to be
updated.

(Incidentally, this is different to how things worked in the COM world. If
you do the same thing in VB6, you'll see that the updated By Ref parameter
value does not get passed back to the property -- this has confused me in
the past too! I do think the .NET way is the more logical and consistent
behaviour)

HTH,

Signature

(O)enone


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.