Abstract Class Property Assignment Overflow

caribou_2000

Member
Joined
Jan 2, 2004
Messages
8
(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 cant 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();
}
 
When you do
C#:
 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.
C#:
class Message : KeyType
{
public Message()
{
msgXML.Type = "MESSAGE";
}

private string _Type;

internal override String Type
{
get
{
return _Type;
}
set
{
_Type = value;
}
}
}
 
Back
Top