caribou_2000 Posted February 3, 2004 Posted February 3, 2004 (Reference the included code.) [Conditions:] I have created an abstract class named KeyType that has only one property in this example. The Message class implements the KeyType class. Upon creation of the Message class, I want to assign a value to the Type property. I have attempted to assign a value to the property inside the Message class upon creation and also external to the class by direct assignment. [issue:] Using the debugger to step thru, I enter the Type property and never leave the set assignment. Eventually I receive a stack overflow error because of the continuous looping of the assignment within the Set part of the property. Using a local watch on the variable in the Set area, I can see that an overflow exception is being thrown. [Question:] Why does the program hang or loop continuously inside of the property assignment? I can't figure it out. [Abstract Class] protected abstract class KeyType { internal KeyType() { } internal abstract String Type { get; set; } } [implementation of Abstract KeyType Class] class Message : KeyType { public Message() { msgXML.Type = "MESSAGE"; } internal override String Type { get { return Type; } set { Type = value; } } } [Function that creates Message Class:] public void SomeFunction() { Message msgXML = new Message(); } Quote
Administrators PlausiblyDamp Posted February 3, 2004 Administrators Posted February 3, 2004 When you do set { Type = value; } you are assigning the value to the property which calls the property set recursively (calls itself). You are probably better of with a private variable to store the real value. class Message : KeyType { public Message() { msgXML.Type = "MESSAGE"; } private string _Type; internal override String Type { get { return _Type; } set { _Type = value; } } } Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
caribou_2000 Posted February 3, 2004 Author Posted February 3, 2004 Thanks for ending my endless loop. I appreciate it. Quote
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.