c# - int.Parse not throwing an exception -
i have custom control contains textbox. custom class has 3 properties, minvalue, maxvalue, , value, defined so:
public int value { { return int.parse(text.text); } set { text.text = value.tostring(); } } public int maxvalue { get; set; } public int minvalue { get; set; } when textbox inside custom class loses focus, following method run:
void text_lostfocus(object sender, eventargs e) { value = value > maxvalue ? maxvalue : value; value = value < minvalue ? minvalue : value; } if textbox has string larger 2,147,483,647, text stays same when focus lost, , no exception thrown.
why exception not thrown, , how can make set values higher int32.maxvalue maxvalue, , values lower int32.minvalue minvalue?
first, there different integer data types in c#.
e.g.
int32 range -2,147,483,648 2,147,483,647
int64 range -9,223,372,036,854,775,808 9,223,372,036,854,775,807
it's technical not possible have greater range using int32 uses 32bits of memory. using unsigned version of type shift range positive values, if don't need negative values, take @ uint32.
to actual increase range, option is, using int64 data type.
see also: https://msdn.microsoft.com/en-us/en-en/library/exx3b86w.aspx
(int alias int32, long int64)
second, neither have try-catch block nor using tryparse there should unhandled system.overflowexception. if there's no exception @ all, that's somehow strange. sure, that's not inside try-catch block?
anyway recommend use tryparse instead of parse , handle errors accordingly.
Comments
Post a Comment