I'd like to define a variable that will accept a string in the SET, but will then convert it to an Int32 and use that during the GET.
Here the code that I currently have:
private Int32 _currentPage;
public String currentPage
{
get { return _currentPage; }
set
{
_currentPage = (string.IsNullOrEmpty(value)) ? 1 : Convert.ToInt32(value);
}
}
I would suggest an explicit Set
method:
private int _currentPage;
public int CurrentPage
{
get
{
return _currentPage;
}
}
public void SetCurrentPage(string value)
{
_currentPage = (string.IsNullOrEmpty(value)) ? 1 : Convert.ToInt32(value);
}
As a side note, your parse method may do better like this:
if (!int.TryParse(value, out _currentPage)
{
_currentPage = 1;
}
This avoids the formatting exceptions.
Be aware that this is really really bad idea to have a property get & set being used against different types. May be couple of methods would make more sense, and passing any other type would just blow up this property.
public object PropName
{
get{ return field; }
set{ field = int.Parse(value);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With