proxy function

  • Thread starter Thread starter Novice
  • Start date Start date
N

Novice

I remember reading some C# documentation at one point that indicated
that you could create a class:

class A
{
public:
int x;
};

such that if you assign a value to the member variable x, another
function will be invoked (aka a proxy function)?

I'm not sure if there is a requirement for class A to extend another
class or how the mechanism works exactly, but basically, it should
allow you to have a function like this:

int returnValue (int parameterInt){
return parameterInt-1;
}

So that when you instatiate an object of type A:
A a;
a.x = 5;

The value of A.x will end up being 4.

Thanks,
Novice
 
Novice said:
I remember reading some C# documentation at one point that indicated : :
such that if you assign a value to the member variable x, another
function will be invoked (aka a proxy function)?

What you may be thinking of is a property's setter.

public class A
{
int xValue;

public int x
{
get
{
return xValue;
}
set
{
xValue = value - 1;
}
}
}

There is an implied parameter to the setter, named 'value'.

You're responsible for supplying a field to use as a backing store,
or otherwise implementing the property getter.
So that when you instatiate an object of type A:
A a;
a.x = 5;

The value of A.x will end up being 4.


This example will give you the behavior you were seeking,

A a;
a.x = 5;
System.Console.WriteLine( a.x); // displays 4.


Derek Harmon
 
Back
Top