Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

(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();

}

  • Administrators
Posted

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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

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