View Full Version : C# Help
VoXoV
15-12-2010, 02:17 PM
Hi, is there any way to have an 'on variable change' event in C#?
I need some way to have a piece of code execute when a certain variable (call it MyVariable) changes.
For example...
private void MyVariable_Change (object sender EventArgs e)
{
Do Stuff Here;
}
I've looked around on the internet, but most of the stuff went right over my head, and I was hoping some of you would have a simple solution.
Thanks :).
Fengol
15-12-2010, 02:36 PM
you won't be able to do an on variable change but why don't you access it through a property?
object var;
object Var
{
get { //some code
return var; }
set { //some code
var = value; }
}
I'm pretty sure you can raise events within properties; I'll have to play and come back to you on that.
Chippit
15-12-2010, 03:00 PM
Fengol's correct. Use a property, raise the event in the set method. It'll be your simplest option.
VoXoV
15-12-2010, 03:20 PM
I have tried doing that, but it doesn't work (perhaps because I'm not sure that I'm using the property correctly?), and I think that's because I'm giving the variable a value in the event, like this...
int MyVariable
{
get
{
return MyVariable;
}
set
{
ChangeMyVariable();
}
}
public void ChangeMyVariable()
{
//MyVariable = changing value
}
For some reason I don't think I'm using the property correctly, it is the first time I've ever used one.
Krummelz
15-12-2010, 03:33 PM
Try this:
int MyVariable
{
get;
set
{
MyVariableChanged(value);
}
}
public void MyVariableChanged(int value)
{
Console.WriteLine("The value changed to " + value.ToString());
}
dislekcia
15-12-2010, 04:10 PM
I have tried doing that, but it doesn't work (perhaps because I'm not sure that I'm using the property correctly?), and I think that's because I'm giving the variable a value in the event, like this...
int MyVariable
{
get
{
return MyVariable;
}
set
{
ChangeMyVariable();
}
}
public void ChangeMyVariable()
{
//MyVariable = changing value
}
For some reason I don't think I'm using the property correctly, it is the first time I've ever used one.
Also, a property is basically an accessor method around a variable that's not accessible. You need to declare another variable that's going to "store" the value you're accessing via the property.
class ExampleClass
{
protected int myVariable;
public int MyProperty
{
get { return myVariable; }
set
{
myVariable = value;
//Do other crap here, like raise the event or whatever...
}
}
//.. rest of your object ..//
}
AndrewJ
15-12-2010, 05:03 PM
Firstly, and most importantly, what version of the .NET Framework are you using?
public int DinosaurCount { get; set; }
DinosaurCount is an ordinary property ("automatically implemented property".)
public int ThisRoboticDinosaurCountMustBeModifiedFromWithinTh isClass { get; private set; }
ThisRoboticDinosaurCountMustBeModifiedFromWithinTh isClass can be retrieved by other calsses, but under normal, sane, 99.999999%-of-the-time circumstances cannot be modified by other classes.
public int DinosaursWithLasersOnTheirHeadsCount
{
get;
set
{
DinosaursWithLasersOnTheirHeadsCount= value * 2; // Because we love them!
}
}
Whenever DinosaursWithLasersOnTheirHeadsCount has it's value changed, we are are actually multiplying it by 12. so DinosaursWithLasersOnTheirHeadsCount = 2; will result in DinosaursWithLasersOnTheirHeadsCount having a value of 4. Please note that
set
{
DinosaursWithLasersOnTheirHeadsCount= value * 2; // Because we love them!
} is actually a method, so you can go absolutely go crazy in there!
public int RoboticDinosaursEatingLasers
{
get;
set
{
alternativeWayOfDoingTheSameThing(value);
}
}
private void alternativeWayOfDoingTheSameThing(int newValue)
{
RoboticDinosaursEatingLasers = newValue * 2; // Because we love them!
}
An alternate way of doing what I did with DinosaursWithLasersOnTheirHeadsCount.
All together now:
public int DinosaurCount { get; set; }
public int ThisRoboticDinosaurCountMustBeModifiedFromWithinTh isClass { get; private set; }
public int DinosaursWithLasersOnTheirHeadsCount
{
get;
set
{
DinosaursWithLasersOnTheirHeadsCount= value * 2; // Because we love them!
}
}
public int RoboticDinosaursEatingLasers
{
get;
set
{
alternativeWayOfDoingTheSameThing(value);
}
}
private void alternativeWayOfDoingTheSameThing(int newValue)
{
RoboticDinosaursEatingLasers = newValue * 2; // Because we love them!
}
VoXoV
15-12-2010, 05:21 PM
None of this seems to do anything at all. What exactly does 'value' represent?
Well, I'm using Microsoft Visual Studio 2008, so I think it's .NET version 3.5.
Chippit
15-12-2010, 05:56 PM
'value' is a special c# keyword used to represent the value passed into a set accessor in a property.
If, for example, you have a property called MyProperty, and you set it like so:
MyProperty = 6;
With the definition of MyProperty which may, for instance, look like so (in its simplest form):
private byte myProperty;
public byte MyProperty
{
get
{
return myProperty;
}
set
{
myProperty = value;
}
}
Then the value keyword will be 6, and the private member (the actual storage for your property value) will be set to 6.
Think of C# properties as compiler shorthand for more traditional methods of getting and setting private class members as you may do in Java or C++.
Equivalent Java code:
private byte myProperty;
public byte getMyProperty() { return myProperty; }
public void setMyProperty(byte value) { myProperty = value; }
Remember that declaring a property this way doesn't actually create a variable to store whatever data you want to store. Neither, in fact, does it require you to. Properties can technically return constant values or calculate them on demand (calculating them is not really a recommended practice, but possible), or you can omit either get/set definition to have the compiler generate them for you (which often inlines when JITted for performance reasons).
The most important thing to remember about properties, though, is to be careful that a property not accidentally reference itself recursively in the set or get accessors, unless you know what you're doing (your example posted here (http://www.nag.co.za/forums/showthread.php?p=314347#post314347) does this). It's a quick and easy way to find out what a stack overflow is, and it's very easy to make this mistake if the private member you're wrapping shares the name of the property (with perhaps just a case change or something, as is common.)
Powered by vBulletin® Version 4.2.4 Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.