[RESOLVED]Seting property value causes tack Overflow state error.

I think I am over looking something extremely simple on this one but just can’t put my finger on it. I am trying to set the value of a public property in my ViewModel but when I do it errors out with "Cannot evaluate expression because the current thread
is in a stack overflow state". What am I missing?

public class PollVM
{
	public PollVM()
	{
		this.Choices = new List<KeyValuePair<string, int>>();
	}

	public int ID { get; set; }
	public string Question { get; set; }
	public bool IsMultipleChocie { get; set; }
	public DateTime? ExperationDate { get; set; }
	public List<KeyValuePair<string, int>> Choices { get; set; }
	public List<string> VotedIPs { get; set; }
	public List<int?> VotedUserIDs { get; set; }
	public bool HasVoted { get; set; }

	public string Title
	{
		get
		{
			return Title.Replace(" ", "-");
		}

		set
		{
			Title = value;
		}
	}
}

Eagle_f90

public string Title
	{
		get
		{
			return Title.Replace(" ", "-");
		}

		set
		{
			Title = value;
		}
	}

When you assign a value to the Title property, your set code is then also assigning the value to the Title property, which calls the set code again which is then also assigning … – hence the stack overflow.

You need to either add a backing variable manually to store that value behind the property (i.e. a private string _title;  ) and then your set code would be _title = value;

for example:

private string _title;
public string Title
	{
		get
		{
			return _title.Replace(" ", "-");
		}

		set
		{
			_title = value;
		}
	}

Or you should leave the setter code empty to allow the compiler add the backing variable for you

i.e

set;

mbanavige

Or you should leave the setter code empty to allow the compiler add the backing variable for you

i.e

set;

When I change it to

public string Title
{
	get
	{
		return Title.Replace(" ", "-");
	}

	set;
}

I get the error "PollVM.Title.set’ must declare a body because it is not marked abstract, extern, or partial"

since you are manipulating the value returned by the get, i’d just go with the explicit backing variable.

sample of that also posted above.

Leave a Reply