Reputation: 197
I get the exception "Input string was not in correct format". I want to handle that exception and add my own error. The input should be an int. Where should I do this? I have an objectdatasource with listview and I'm having trouble getting the textbox.text from the code behind so I can use tryParse.
Upvotes: 2
Views: 2446
Reputation: 224
'value' will always be of the same type as your variable. Thus having this:
private bool mabool = false;
public bool MaBool
{
get { return mabool; }
set { mabool = value; }
}
Won't ever crash. This because, as I said, value will be the same type of the variable. In this case, value is a boolean.
Try it with a class:
public class Rotator
{
public Roll, Pitch, Yaw;
// Declarations here (...)
}
private Rotator rotation = new Rotator();
public Rotator Rotation
{
get { return rotation; }
set
{
// Since value is of the same type as our variable (Rotator)
// then we can access it's components.
if (value.Yaw > 180) // Limit yaw to a maximum of 180°
value.Yaw = 180;
else if (value.Yaw < -180) // Limit yaw to a minimum of -180°
value.Yaw = -180;
rotation = value;
}
}
As seen on the second example, value is a Rotator, thus we can access it's components.
Upvotes: 1
Reputation: 39013
Number
is always an int
, it is defined that way...
You probably want to validate the content of a string. Easiest way is to parse it into an int
:
int number;
if(!int.TryParse(yourString, out number))
{
Not an int!
}
Upvotes: 2
Reputation: 1038730
Your property is of type Int32. You cannot assign anything else than a valid integer to this property. Now if you have some user input which is under the form of a string and then you need to assign it to the integer property you could use the int.TryParse method to ensure that the value entered by the user is a valid integer.
For example:
string someValueEnteredByUser = ...
int value;
if (!int.TryParse(someValueEnteredByUser, out value))
{
// the value entered by the user is not a valid integer
}
else
{
// the value is a valid integer => you can use the value variable here
}
Upvotes: 2