473,763 Members | 8,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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!=v alue1) Console.WriteLi ne("value1 is changed");
if(oldvalue2!=v alue2) Console.WriteLi ne("value2 is changed");
oldvalue1=value 1;
oldvalue2=value 2;
}
}
class myprogram
{
MyClass nn = new MyClass()
nn.value1=2;
nn.WhatIsChange d();
}
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=(MyCl ass)this.Member wiseClone();
}
public void WhatIsChanged()
{
if(oldvalues.va lue1!=this.valu e1) Console.WriteLi ne("value1 is
changed");
if(oldvalues.va lue2!=this.valu e2) Console.WriteLi ne("value2 is
changed");
oldvalues=(MyCl ass)this.Member wiseClone();
}
}
class myprogram
{
MyClass nn = new MyClass()
nn.value1=2;
nn.WhatIsChange d();
}

But this don't work!

Jan 24 '06 #1
4 1367
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.c om> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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!=v alue1) Console.WriteLi ne("value1 is changed");
if(oldvalue2!=v alue2) Console.WriteLi ne("value2 is changed");
oldvalue1=value 1;
oldvalue2=value 2;
}
}
class myprogram
{
MyClass nn = new MyClass()
nn.value1=2;
nn.WhatIsChange d();
}
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=(MyCl ass)this.Member wiseClone();
}
public void WhatIsChanged()
{
if(oldvalues.va lue1!=this.valu e1) Console.WriteLi ne("value1 is
changed");
if(oldvalues.va lue2!=this.valu e2) Console.WriteLi ne("value2 is
changed");
oldvalues=(MyCl ass)this.Member wiseClone();
}
}
class myprogram
{
MyClass nn = new MyClass()
nn.value1=2;
nn.WhatIsChange d();
}

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=(MyCl ass)this.Member wiseClone();
}
public void WhatIsChanged()
{
if(oldvalues.va lue1!=this.valu e1) Console.WriteLi ne("value1
is changed");
if(oldvalues.va lue2!=this.valu e2) Console.WriteLi ne("value2
is changed");
oldvalues=(MyCl ass)this.Member wiseClone();
}
}
/// <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******** ******@tk2msftn gp13.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!=v alue1) Console.WriteLi ne("value1 is changed");
if(oldvalue2!=v alue2) Console.WriteLi ne("value2 is changed");
oldvalue1=value 1;
oldvalue2=value 2;
}
}
class myprogram
{
MyClass nn = new MyClass()
nn.value1=2;
nn.WhatIsChange d();
}
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=(MyCl ass)this.Member wiseClone();
}
public void WhatIsChanged()
{
if(oldvalues.va lue1!=this.valu e1) Console.WriteLi ne("value1 is
changed");
if(oldvalues.va lue2!=this.valu e2) Console.WriteLi ne("value2 is
changed");
oldvalues=(MyCl ass)this.Member wiseClone();
}
}
class myprogram
{
MyClass nn = new MyClass()
nn.value1=2;
nn.WhatIsChange d();
}
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.Value1 Changed += new MyClass.Integer ChangedHandler( original_Value1 Changed);
original.Value2 Changed += new MyClass.Integer ChangedHandler( original_Value2 Changed);

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

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

Console.ReadLin e();
}

private void original_Value1 Changed(int oldValue, int newValue)
{
Console.WriteLi ne("Value1 has changed from {0} to {1}.", oldValue, newValue);
}

private void original_Value2 Changed(int oldValue, int newValue)
{
Console.WriteLi ne("Value2 has changed from {0} to {1}.", oldValue, newValue);
}
}

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

public event IntegerChangedH andler Value1Changed;
public event IntegerChangedH andler 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)
{
IntegerChangedH andler handler = Value1Changed;

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

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

if (handler != null)
{
handler(oldValu e, newValue);
}
}
}
Jan 24 '06 #5

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

Similar topics

11
16224
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? 1) simply UPDATING the values for all fields in the table, whether or not any particular field has actually changed 2) running a second SELECT statement and comparing the $_POST vars to the returned values, and only UPDATING those that have...
0
1125
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 reference parameter by the method of the class. For example, an COM class named myclass ,it has a method named
9
7975
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 our controls when they're visible and so if a tab page is changed, I would like for the control to know it's no longer visible and release that lock. Currently that doesn't happen. Does anybody have any suggestions and/or is this a bug? Lance...
9
2284
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) as a popup using either window.open or window.showModalDialog. Since I want to warn the users of my application when they try to close page1.aspx and have changed the values in the meantime I thought about using the “onbeforeunload” event of the...
2
1243
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 client applications again.
8
7034
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 more errors encountered while loading the designer. The errors
5
3464
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 changed to enable/disable the save button. Is there a way of telling if a dataset has been changed (ds property?) other than using this method? Any help greatly appreciated! Thanks in advance
20
1610
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 But when compiled and run under VS 2005: you get: Control.Paint
2
1221
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 to a Form, the code-behind file contains code for each item. This seems unecessary and causes items to be added more than once (first, because of my ComboBox constructor; again, because of the code-behind items). I suspect that there may be a...
0
9564
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9387
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9823
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.