OO Class Event

joe_pool_is

Contributor
Joined
Jan 18, 2004
Messages
507
Location
Longview, TX [USA]
I'm interested in creating an event in my inherited class.

Given this:
Code:
public abstract class ZBase {
  string _test;
  public ZBase() {
    _test = string.Empty;
  }
  public string TestString { get { return _test; } set { _test = value; } }
}

public class MyData1 : ZBase {
  public enum DbType = { None=0, This=1, That=2 };
  DbType _type;
  public MyData1() {
    _type = DbType.None;
  }
}

public class MyData2 : ZBase {
  public enum DbType = { None=0, Here=1, There=2, Expired=4 };
  DbType _type;
  public MyData2() {
    _type = DbType.None;
  }
}

I want to add some kind of event handler that can react whenever TestString is set.

Example1:
Code:
MyData1 data1 = new MyData1();
data1.TestString = "I am that";
Now, with some event handler, data1's _type should be DbType.That.

Example 2:
Code:
MyData2 data2 = new MyData2();
data2.TestString = "I am here";
Now, with some event handler, data2's _type should be DbType.Here.

The event handler would parse the text in the string using something similar to this:
Code:
// some kind of event handler:
if (-1 < TestString.IndexOf("that")) {
  _type = DbType.That;
}

I don't know how create an event handler for a variable, though.

Obviously, the class I'd use this for is a little more complicated than that. The enumeration would be a type of product, type of employee, type of business, type of process, etc.

I appreciate your help.
~Joe
 
Personally I have few experience in C# but in VB.NET you do it like this:

You first create a variable and an event. p.e.
Code:
Public Event StringChanged(ByVal sender As Object, ByVal e As  System.EventArgs)
Or with a delegate, that's up to you.

Then you create a property in within the property you call
Code:
RaiseEvent StringChanged(Me, New System.EventArgs)
in the set section of the property

You can now add eventhandlers with
Code:
addhandler me.StringChanged, addressof MyStringChangedEventHandler
if your event handling function was called MyStringChangedEventHandler.

Hope it's usefull to you, though it's VB.NET

~DP

ps. for inherited classes you can also make your handling function overridable. This way you can override the handling function in the child classes.
 
Last edited:
I think the best method in an inheritance scenario is a virtual method. The inheriting class can override the method to add logic and then call the base method.

In your example, you could have the prorerty setter call a virtual method that inheritors override. The following would work with the strings you used ("I am xxx").
Code:
public abstract class ZBase {
  string _test;
  public ZBase() {
    _test = string.Empty;
  }
  public string TestString { get { return _test; } 
      set { 
          _test = value; 
          [COLOR="Red"]OnTestStringSet(value);[/COLOR]
      } 
  }

[COLOR="Red"]  protected virtual void OnTestStringSet(string value) {
  }[/COLOR]
}

public class MyData1 : ZBase {
  public enum DbType = { None=0, This=1, That=2 };
  DbType _type;
  public MyData1() {
    _type = DbType.None;
  }

[COLOR="Red"]  protected overrides void OnTestStringSet(string value) {
      _type = (DbType)Enum.Parse(typeof(DbType), value.Split(' ')[2]);
  }[/COLOR]
}

public class MyData2 : ZBase {
  public enum DbType = { None=0, Here=1, There=2, Expired=4 };
  DbType _type;
  public MyData2() {
    _type = DbType.None;
  }


[COLOR="Red"]  protected overrides void OnTestStringSet(string value) {
      _type = (DbType)Enum.Parse(typeof(DbType), value.Split(' ')[2]);
  }
[/COLOR]
}
 
Back
Top