Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm interested in creating an event in my inherited class.

 

Given this:

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:

MyData1 data1 = new MyData1();
data1.TestString = "I am that";

Now, with some event handler, data1's _type should be DbType.That.

 

Example 2:

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:

// 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

Posted (edited)

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.

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

RaiseEvent StringChanged(Me, New System.EventArgs)

in the set section of the property

 

You can now add eventhandlers with

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.

Edited by DPrometheus

My Development System

Intel Core i7 920 @2.66Ghz

6 GB DDR3 SDRAM

Windows 7 Ultimate x64 & Windows Vista Home Premium x64 dual boot

GeForce GTX295 1.8 GB

3.5 TB HD

  • Leaders
Posted

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").

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]
}

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...