Groups | Blog | Home
all groups > c# > november 2006 >

c# : Important method in setter.


Jesse McGrew
11/25/2006 3:13:49 PM
[quoted text, click to view]

No, it won't. "a = new A();" only affects the local variable "a", *not*
anything in c. "c.A" will still return the old value, because all
access to the private field c.a is protected by the property get/set
methods.

Jesse
Peter Duniho
11/25/2006 4:27:17 PM
[quoted text, click to view]

Maybe if you were more specific about what you're trying to do, people could
give you better answers.

Your question appears to be saying that anytime you assign a reference of
type A to a variable, you want "important_method()" to be called. This is a
very unusual requirement, and probably indicates a failure with respect to
the design of the code. What is in "important_method()" that you feel needs
to be run every time you assign a reference of type A to a variable?

In C++, you could overload the assignment operator. I don't recall if this
is possible in C#, but if it is you might be able to do what you want doing
that. However, it still begs the question of why it is you think this very
unusual practice needs to occur in your own code. More than likely, it
shouldn't.

And if you *don't* want your "important_method()" to be called every time
you assign a reference of type A to a variable, then you have asked the
wrong question. Rewording the question so that you ask the question you
intended will help, in that case.

Pete

kruskal
11/25/2006 11:56:47 PM
Hi,

if i have:

private a;
public T A {
get {
return a;}
set {
important_method(a, value);
a =3D value;}
}

in class C...

Anyone can do:

C c =3D new C();

A a =3D c.A;

a =3D new A();

And a will change without important_method(a, new A());

How can I prevent it?

-- =

Pozdrawiam,
Jon Skeet [C# MVP]
11/26/2006 12:00:00 AM
[quoted text, click to view]

Use your property within the constructor of A.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
kruskal
11/26/2006 12:51:34 AM
Dnia 26-11-2006 o 00:13:49 Jesse McGrew <jmcgrew@gmail.com> napisał(a):

[quoted text, click to view]

So what should I do to a = new A(); will raise important_method()?

--
Pozdrawiam,
Dave Sexton
11/26/2006 1:31:11 AM
Hi Peter,

[quoted text, click to view]

It isn't possible in C# to overload assignment (=), but an OOP approach will
certainly be a better choice anyway, if in fact the expected behavior
"should" be occurring in the first place :)

--
Dave Sexton

Prakash Nadar
11/26/2006 10:08:40 PM
[quoted text, click to view]

hello, i just looked up on important_method and i could not find
anything about it.

What is important_method.

I have started learning C# just a couple of days ago, so i am newbie :)

AddThis Social Bookmark Button