set
{
this.IsSelectableNode = value; // throws error
}
As it is causing recursive calling for Set.
You either make this property as Readonly by removing Set, as in Get you are driving value from other Property (this.NodeType) OR add one private variable
for that.
Somnath Mali
.NET Developer , Pune INDIA.
Please Mark As Answer If my reply helped you.
Yes, i don't need a ReadOnly property because i won't be able to override it then.
How will i use it with a private field?
public class TreeNode
{
public int NodeType { get; set; }
bool isSelectableNode { get; set; } // How do i set the default value?
public bool IsSelectableNode
{
get
{
return isSelectableNode;
}
set
{
isSelectableNode = value;
}
}
}
public class TreeNode
{
public int NodeType {get;set;}
private bool? _isSelectable;
public bool IsSelectable
{
get { return _isSelectable ?? NodeType == 1; }
set { _isSelectable = value; }
}
}
_isSelectable is of type nullable bool. It will default to null. The get for IsSelectable will use the values of _isSelectable if it is not null (that is if it has been set by a caller). If it is null it will defaul to the result of NodeType ==1. When
you want to override you can use .IsSelectable = true;
zuperboy90
Participant
977 Points
819 Posts
C# Get / Set ussage
Apr 26, 2012 06:51 AM|LINK
Hello,
I have a class with a property
public class TreeNode { public int NodeType { get; set; } public bool IsSelectableNode { get { if(this.NodeType == 1) return true; return false; } set { this.IsSelectableNode = value; // throws error } } }I want to be able to override the value of IsSelectableNode when i need to.
var node = new TreeNode // IsSelectableNode = true { NodeType = 1 } var node2 = new TreeNode { NodeType = 2, IsSelectableNode = false // override the default property }Is this possible?
Thank you
somnathmali
Contributor
2816 Points
450 Posts
Re: C# Get / Set ussage
Apr 26, 2012 06:58 AM|LINK
Remove the
set { this.IsSelectableNode = value; // throws error }As it is causing recursive calling for Set.
You either make this property as Readonly by removing Set, as in Get you are driving value from other Property (this.NodeType) OR add one private variable for that.
.NET Developer , Pune INDIA.
Please Mark As Answer If my reply helped you.
zuperboy90
Participant
977 Points
819 Posts
Re: C# Get / Set ussage
Apr 26, 2012 07:01 AM|LINK
Yes, i don't need a ReadOnly property because i won't be able to override it then.
How will i use it with a private field?
public class TreeNode { public int NodeType { get; set; } bool isSelectableNode { get; set; } // How do i set the default value? public bool IsSelectableNode { get { return isSelectableNode; } set { isSelectableNode = value; } } }zuperboy90
Participant
977 Points
819 Posts
Re: C# Get / Set ussage
Apr 26, 2012 07:08 AM|LINK
I can do this using the constructor, where i add default value for the property and this allows me to override it.
I was wondering if there is a way of doing this without using the constructor
Paul Linton
Star
13421 Points
2535 Posts
Re: C# Get / Set ussage
Apr 26, 2012 07:16 AM|LINK
Do you want something like this?
public class TreeNode { public int NodeType {get;set;} private bool? _isSelectable; public bool IsSelectable { get { return _isSelectable ?? NodeType == 1; } set { _isSelectable = value; } } }_isSelectable is of type nullable bool. It will default to null. The get for IsSelectable will use the values of _isSelectable if it is not null (that is if it has been set by a caller). If it is null it will defaul to the result of NodeType ==1. When you want to override you can use .IsSelectable = true;
zuperboy90
Participant
977 Points
819 Posts
Re: C# Get / Set ussage
Apr 26, 2012 07:28 AM|LINK
Exactly this
Thank you