473,396 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Important method in setter.

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.
Nov 25 '06 #1
6 2517
kruskal wrote:
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

Nov 25 '06 #2
Dnia 26-11-2006 o 00:13:49 Jesse McGrew <jm*****@gmail.comnapisał(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.
So what should I do to a = new A(); will raise important_method()?

--
Pozdrawiam,
Odwrotnie.
Nov 25 '06 #3
"kruskal" <kr*****@gazeta.plwrote in message
news:op***************@odwrotnie.kolo.pl...
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
Nov 26 '06 #4
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 :)

--
Dave Sexton
Nov 26 '06 #5
kruskal <kr*****@gazeta.plwrote:
Dnia 26-11-2006 o 00:13:49 Jesse McGrew <jm*****@gmail.comnapisa?(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.

So what should I do to a = new A(); will raise important_method()?
Use your property within the constructor of A.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 26 '06 #6
kruskal wrote:
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
Nov 26 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Carlos | last post by:
Hi! I want to instrumentate a class with a number of getter/setters. Each pair of getter/setter must keep it's own state gsState but also access to the state iState of instances of the...
5
by: Flipje | last post by:
In my view, there is a major drawback to using attributes: the getter and the setter have identical protection levels. But I usually want the getter to be public and the setter to be protected or...
3
by: Bruce Wood | last post by:
Maybe I'm going nuts, but I was so sure that adding the same method more than once to a delegate would result in only one entry on the delegate's call list: this.UpdateEnd += new...
5
by: Greg Scharlemann | last post by:
For some reason, I am having trouble retrieving the data that I store in the object that I have created from a database query. I created this class Lead (see below). In the php page, I create and...
4
by: jarek | last post by:
Hi, this is my code: CSSStyleDeclaration.prototype.__defineSetter__('display', displaySetter); function displaySetter(value) { var parent = findParent(document, this); if (parent) {
12
by: Adam Sandler | last post by:
Hi all, I hope this is an easy one... Using VWD 2005. When I call my accessor method (getName) I always receive an empty string back. Debugging shows there should be something there but I...
3
by: Martin Pöpping | last post by:
Hello, I´m coming from the Java World. Here Programmers often use (like in C++?) getter and setter methods. F.e.: class Mirror{ private int width_;
0
by: shyamg | last post by:
Hi all i am newly add new Attribute "name" in strutshtml- tld file but its asking for setter method for attribute. where can add the setter and getter. Thanks. ss.
5
by: javatech007 | last post by:
Hi, I've been at this question all day and still just don't know what to do. It's the last of all the questions I have to do and can't figure it out!! If anyone could help or give guidance it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.