Important method in setter.

  • Thread starter Thread starter kruskal
  • Start date Start date
K

kruskal

Hi,

if i have:

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

in class C...

Anyone can do:

C c = new C();

A a = c.A;

a = new A();

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

How can I prevent it?
 
kruskal said:
Hi,

if i have:

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

in class C...

Anyone can do:

C c = new C();

A a = c.A;

a = new A();

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

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
 
Dnia 26-11-2006 o 00:13:49 Jesse McGrew said:
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.

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

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
 
Hi Peter,
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.

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 :)
 
kruskal said:
Hi,

if i have:

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

in class C...

Anyone can do:

C c = new C();

A a = c.A;

a = new A();

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

How can I prevent it?

--Pozdrawiam,
Odwrotnie.

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 :)

-Prakash
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top