简体   繁体   中英

int.Parse not throwing an exception

I have a custom control that contains a TextBox. The custom class has 3 properties, MinValue, MaxValue, and Value, defined as so:

public int Value
{
    get { return int.Parse(text.Text); }
    set
    {
        text.Text = value.ToString();
    }
}

public int MaxValue { get; set; }
public int MinValue { get; set; }

When the TextBox inside the custom class loses focus, the following method is run:

void text_LostFocus(object sender, EventArgs e)
{
    Value = Value > MaxValue ? MaxValue : Value;
    Value = Value < MinValue ? MinValue : Value;
}

If the TextBox has a string that is larger than 2,147,483,647, the text stays the same when focus is lost, and no exception is thrown.

Why is the exception not thrown, and how can I make it so it will also set values higher than Int32.MaxValue to MaxValue , and values lower than Int32.MinValue to MinValue ?

First, there a are different integer data types in C#.

eg

Int32 with a range from -2,147,483,648 to 2,147,483,647

Int64 with a range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

It's technical not possible to have a greater range using an Int32 as it uses 32bits of memory. Using an unsigned version of the type will shift the range to only positive values, so if you don't need negative values, you could also take a look at an UInt32 .
To actual increase the range, your only option is, using an Int64 data type.

see also: https://msdn.microsoft.com/en-US/en-en/library/exx3b86w.aspx

( int is an alias for Int32 , long for Int64 )

Second, as you neither have a try-catch block nor using TryParse there should be an unhandled System.OverflowException . If there's no exception at all, that's somehow strange. Are you sure, that's not inside a try-catch block?

Anyway I would recommend to use TryParse instead of Parse and handle errors accordingly.

If I were in your situation, and wanted to understand what was going on, I would run the app under the debugger, with a breakpoint in the LostFocus event handler. You could also consider adding a try/catch/throw, which might make it easier verify that an exception is indeed being thrown, and if so to inspect it before it is swallowed:

void text_LostFocus(object sender, EventArgs e)
{
    try
    {
        Value = Value > MaxValue ? MaxValue : Value;
        Value = Value < MinValue ? MinValue : Value;
    }
    catch(Exception ex)
    {
        throw;
    }
}

Okay so I found the issue:

在此处输入图片说明

The checkbox inside the red circle unchecked itself. Could've been when I repaired the installation of my IDE (VS Express 2012). I recently installed VS 2012 Professional and they use the same options because checking it in Professional also fixed it in Express

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM