Changed class

  • Thread starter Thread starter GTi
  • Start date Start date
G

GTi

I'm trying to build a class that can track changes to itself.

Class MyClass
{
public int value1=0;
public int value2=0;
private int oldvalue1=0;
private int oldvalue2=0;

public void WhatIsChanged()
{
if(oldvalue1!=value1) Console.WriteLine("value1 is changed");
if(oldvalue2!=value2) Console.WriteLine("value2 is changed");
oldvalue1=value1;
oldvalue2=value2;
}
}


class myprogram
{
MyClass nn = new MyClass()
nn.value1=2;
nn.WhatIsChanged();
}


But there MUST be a simpler way of doing this?


So if I use MemberwiseClone(); will it be like?
Class MyClass
{
public int value1=0;
public int value2=0;
private oldvalues=null;

MyClass()
{
oldvalues=(MyClass)this.MemberwiseClone();
}


public void WhatIsChanged()
{
if(oldvalues.value1!=this.value1) Console.WriteLine("value1 is
changed");
if(oldvalues.value2!=this.value2) Console.WriteLine("value2 is
changed");
oldvalues=(MyClass)this.MemberwiseClone();
}
}


class myprogram
{
MyClass nn = new MyClass()
nn.value1=2;
nn.WhatIsChanged();
}

But this don't work!
 
Do something like (Apologies to those that can't stomach symbols that differ
by case alone...):

public class xxClass {
private string val1 = "";
private string val2 = "";
private string oldVal1 = "";
private string oldVal2 = "";

public string Val1 {
get {return val1;}
set {
oldVal1 = val1;
val1 = value;
}
}

public string Val2 {
get {return val2;}
set {
oldVal1 = val2;
val2 = value;
}
}

public void whatsChanged() {
if (val1 != oldVal1) {
// val1 changed
}
if (val2 != oldVal2) {
// val2 changed
}
}

}
 
The above code does work, with some changes. Below is the code that i
ran, and it seems to do the job:

class MyClass
{
public int value1 = 0;
public int value2 = 0;
private MyClass oldvalues = null;

public MyClass()
{
oldvalues=(MyClass)this.MemberwiseClone();
}


public void WhatIsChanged()
{
if(oldvalues.value1!=this.value1) Console.WriteLine("value1
is changed");
if(oldvalues.value2!=this.value2) Console.WriteLine("value2
is changed");
oldvalues=(MyClass)this.MemberwiseClone();
}
}


/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
MyClass c = new MyClass ();
c.value1 = 2;
c.WhatIsChanged ();
}

Whats the exact error that you get when you try this approach?

- NuTcAsE
 
Gabriel Magaña said:
Do something like (Apologies to those that can't stomach symbols that
differ by case alone...):

public class xxClass {
private string val1 = "";
private string val2 = "";
private string oldVal1 = "";
private string oldVal2 = "";

public string Val1 {
get {return val1;}
set {
oldVal1 = val1;
val1 = value;
}
}

Depends on the behaviour required - this would see "a"->"b"->"a" as a change
which may not be what is required.

I have no idea why the OP thinks there MUST be an easier way to do it.

My personal suggestion is to throw a wrapper around a DataRow as it has all
the all the functionality required.
 
I'm trying to build a class that can track changes to itself.
Class MyClass
{
public int value1=0;
public int value2=0;
private int oldvalue1=0;
private int oldvalue2=0;
public void WhatIsChanged()
{
if(oldvalue1!=value1) Console.WriteLine("value1 is changed");
if(oldvalue2!=value2) Console.WriteLine("value2 is changed");
oldvalue1=value1;
oldvalue2=value2;
}
}
class myprogram
{
MyClass nn = new MyClass()
nn.value1=2;
nn.WhatIsChanged();
}
But there MUST be a simpler way of doing this?

So if I use MemberwiseClone(); will it be like?
Class MyClass
{
public int value1=0;
public int value2=0;
private oldvalues=null;
MyClass()
{
oldvalues=(MyClass)this.MemberwiseClone();
}
public void WhatIsChanged()
{
if(oldvalues.value1!=this.value1) Console.WriteLine("value1 is
changed");
if(oldvalues.value2!=this.value2) Console.WriteLine("value2 is
changed");
oldvalues=(MyClass)this.MemberwiseClone();
}
}
class myprogram
{
MyClass nn = new MyClass()
nn.value1=2;
nn.WhatIsChanged();
}
But this don't work!

I would opt for more of a observer (.NET style) type of pattern. And let
another object be responsible for tracking changes.

public class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main()
{
new Class1();
}

public Class1()
{
MyClass original = new MyClass(5, 2);
original.Value1Changed += new MyClass.IntegerChangedHandler(original_Value1Changed);
original.Value2Changed += new MyClass.IntegerChangedHandler(original_Value2Changed);

original.Value1 = 32;
original.Value2 = 1024;

original.Value1 = 5;
original.Value2 = 2;

Console.ReadLine();
}

private void original_Value1Changed(int oldValue, int newValue)
{
Console.WriteLine("Value1 has changed from {0} to {1}.", oldValue, newValue);
}

private void original_Value2Changed(int oldValue, int newValue)
{
Console.WriteLine("Value2 has changed from {0} to {1}.", oldValue, newValue);
}
}

public class MyClass
{
public delegate void IntegerChangedHandler(int oldValue, int newValue);

public event IntegerChangedHandler Value1Changed;
public event IntegerChangedHandler Value2Changed;

private int value1;
private int value2;

public MyClass()
{}

public MyClass(int value1, int value2)
{
this.value1 = value1;
this.value2 = value2;
}


public int Value1
{
get { return value1; }
set
{
OnValue1Changed(value1, value);

value1 = value;
}
}

public int Value2
{
get { return value2; }
set
{
OnValue2Changed(value2, value);

value2 = value;
}
}

protected virtual void OnValue1Changed(int oldValue, int newValue)
{
IntegerChangedHandler handler = Value1Changed;

if (handler != null)
{
handler(oldValue, newValue);
}
}

protected virtual void OnValue2Changed(int oldValue, int newValue)
{
IntegerChangedHandler handler = Value2Changed;

if (handler != null)
{
handler(oldValue, newValue);
}
}
 
Back
Top