joe_pool_is Posted April 9, 2009 Posted April 9, 2009 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 Quote Avoid Sears Home Improvement
DPrometheus Posted April 10, 2009 Posted April 10, 2009 (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 April 10, 2009 by DPrometheus Quote 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
joe_pool_is Posted April 13, 2009 Author Posted April 13, 2009 That got me on the right track. Thanks DP. Quote Avoid Sears Home Improvement
Leaders snarfblam Posted April 13, 2009 Leaders Posted April 13, 2009 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] } Quote [sIGPIC]e[/sIGPIC]
joe_pool_is Posted April 13, 2009 Author Posted April 13, 2009 I like that idea a lot, actually! Thanks! That isn't the first time you've given me some good ideas on here, either. Quote Avoid Sears Home Improvement
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.