473,378 Members | 1,405 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,378 software developers and data experts.

Changed class

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!

Jan 24 '06 #1
4 1353
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
}
}

}
"GTi" <tu****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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!

Jan 24 '06 #2
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

Jan 24 '06 #3

"Gabriel Magaña" <no*****@no-spam.com> wrote in message
news:ek**************@tk2msftngp13.phx.gbl...
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.

Jan 24 '06 #4
> 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_Value1Chang ed);
original.Value2Changed += new MyClass.IntegerChangedHandler(original_Value2Chang ed);

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);
}
}
}
Jan 24 '06 #5

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

Similar topics

11
by: Jason | last post by:
Let's say I have an html form with 20 or 30 fields in it. The form submits the fields via POST to a php page which updates a table in a database with the $_POST vars. Which makes more sense? ...
0
by: peter | last post by:
Hi all, In VB.net,using Type.InvokeMember to call a method of a class in COM dll,the method has a reference parameter,It seems that there is no way to return the changed value of that...
9
by: Lance Johnson | last post by:
I would like for when a tab page changes, that my controls on the previous tab page would receive a visiblechanged event or some other event so they are aware of that. We have db locks in some of...
9
by: Daniel Walzenbach | last post by:
Hi I am faced with the following problem: I have a page (let’s call this page page1.aspx) containing some TextBoxes and a hyperlink which opens another page (let’s call this page page2.aspx)...
2
by: A.M | last post by:
Hi, I had a web server contained a web service. We had to change the web server computer; so all client proxies must point to new web server. My understanding is we have to compile all...
8
by: dbuchanan | last post by:
Hello, What does this error mean? "The event click is read-only and cannot be changed" This is a design-time error. It is displayed instead of the form. Here is the full text \\ "One or...
5
by: Mark | last post by:
Hello, I have a form where I want to only enable a save button when something has changed in the dataset. Currently I'm checking current values against original values everytime a value is...
20
by: Andy DeMaurice | last post by:
Something has changed from C# 1.0 to 2.0 regarding interface re-implementation. See my sample code below; when compiled and run under VS 2003, you get: Control.Paint Control.IControl.Paint ...
2
by: =?Utf-8?B?SmFzb24gQmFybmV0dA==?= | last post by:
I've created a component that inherits from ComboBox. Wiithin its constructor, I've hardcoded some initial items that I'd like added. These items are of a custom class. When I add my control...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.