Kuthuil1
Kuthuil1

Reputation: 23

C# code returns out of method when value is too large

I got a small method that splits a string into two parts, retrieves one part and uses it to establish a connection to a server:

private void RetrieveData(string vesselCode)
{
    string[] splitSelectedVessel = vesselCode.Split('_');
    int selectedVessel = int.Parse(splitSelectedVessel[1]);

    // URL of server to retrieve data.
    // constructed with vesselCode.
    string baseUrl = ".../vehicle/";
    string jsonUrl = baseUrl + selectedVessel;

    PtVehicle currentVessel = DownloadAndDeserialize<PtVehicle>(jsonUrl);

}

VesselCode containts two parts, combined with an '_' in between:

Both are of entry fields from a Xamarin project and are originally stored as strings.

Whenever the VesselNumber part gets too large, it just stops right after int selectedVessel = int.Parse(splitSelectedVessel[1]);. It seems to break out of the method above, without giving a warning or anything. I've tested it with breakpoints on every line. It simply stops running that method and goes on with whatever it was doing, often letting the app crash later on (since it needs that value). What is happening here?

Upvotes: 1

Views: 1029

Answers (4)

Kuthuil1
Kuthuil1

Reputation: 23

I ended up fixing the issue with @Ilya Chernomordik's and the documentation provided by @Mihai.

The issue was that int selectedVessel = int.Parse(splitSelectedVessel[1]); parses the value of that string to an int. When the value was larger than the int max size, it crashed. Since VesselNumber is an ID and I don't have to calculate it, I simply solved it by storing it as a string:

string selectedVessel = splitSelectedVessel[1];

Another way is to store it as a long (int) of 64 bits, but this only "moves" the issue to a much larger number.

Upvotes: 1

Mihai
Mihai

Reputation: 471

It should throw an System.OverflowException if the number is too big to be parsed as int. According to Microsoft documentation long/ ulong has the biggest range.

You might have breaking for exceptions disabled if you are using Visual Studio so you might want to enable them to see what's the actual problem Debug->Windows->Exception Settings and make sure Common Language Runtime Exceptions are checked.

On the other hand, the best practice is to use the .TryParse approach when converting to an integral type.

Upvotes: 0

AQuirky
AQuirky

Reputation: 5236

What is happening here is that you are throwing an exception. Consider the following code...

        try
        {
            string s1 = "2147483649";
            int j = int.Parse(s1);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

2147483649 is int.MaxValue + 1

This code generates the following message:

Value was either too large or too small for an Int32.

Upvotes: 0

Ilya Chernomordik
Ilya Chernomordik

Reputation: 30205

I suppose that your number is all too long for an int. I don't know what can the maximum number be, but you can try long instead. Probably it's enough.

If it isn't then you have to do something with handling of too large numbers. You can write

if (long.TryParse(myString, out myValue)
{
   // Your logic here
}
else
{
   // Do something if the value is too large
}

Upvotes: 2

Related Questions