-1

I have the following code which create an object from array of string.

struct Item
    {
        public Int32 ItemID { get; }
        public String ItemText { get; }
        public Int32 CategoryID { get;}
        public Int32 GroupID { get;}
        public String LargePictureName { get; }

        public Item(string[] menuItem)
        {
            DateTime minDateTime = System.Data.SqlTypes.SqlDateTime.MinValue.Value;

            ItemID = String.IsNullOrEmpty(menuItem[0]) ? 0 : Convert.ToInt32(menuItem[0]);
            ItemText = menuItem[1];
            CategoryID = String.IsNullOrEmpty(menuItem[2]) ? 0 : Convert.ToInt32(menuItem[2]);
            GroupID = String.IsNullOrEmpty(menuItem[3]) ? 0 : Convert.ToInt32(menuItem[3]);
            LargePictureName = menuItem[4];

But when I run this code I got an Exception "System.FormatException: 'Input string was not in a correct format.'" from this line LargePictureName = menuItem[4]; This is stack trace

Exception thrown: 'System.FormatException' in mscorlib.dll System.Transactions Critical: 0 : http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/UnhandledUnhandled exceptionAMenuImportTool.exeSystem.FormatException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.Convert.ToInt32(String value) ... I confused because there is no conversion on that line. I tried debug it and found that menuItem[4] is empty string. I tried change this line to LargePictureName = "1"; but I still got the same exception. I just don't understand there is no conversion in this line but why I got an exception from conversion to Int32. Any help would be appreciated. Thanks in advance

  • I wonder if you saw the question yours is a duplicate of, since your code is similar to the accepted answer, but you missed the part of the answer that said _"and throw an exception on poorly formatted input"_? – ProgrammingLlama Feb 04 '20 at 03:10
  • 1
    It turned out that I really have problem with conversion. But it's not from this line. My original code is longer and has similar conversion as above code that I pasted. Visual Studio tried to point me to this line which confused me for an hour. – user12836297 Feb 04 '20 at 17:33

1 Answers1

1

Your issue is quite simple, however it's not on the line you think it is, and likely here, or before.

GroupID = String.IsNullOrEmpty(menuItem[3]) ? 0 : Convert.ToInt32(menuItem[3])

Basically one of your menuItems is not an actual number.

If this is user input, it's always better to use int.TryParse to validate it before hand instead of just throwing on invalid input.

Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.

A slightly more robust way of doing this might be

int res = 0;
ItemID = int.TryParse(menuItem[0], out res ) ? res : 0;
ItemText = menuItem[1];
CategoryID = int.TryParse(menuItem[2], out res ) ? res : 0;
GroupID = int.TryParse(menuItem[3], out res ) ? res : 0;

Note : if you are not expecting null or a numeric value, you should investigate why this is failing thoroughly first


Additional Resources

Convert.ToInt32(String)

Converts the specified string representation of a number to an equivalent 32-bit signed integer.

Exceptions

FormatException

value does not consist of an optional sign followed by a sequence of digits (0 through 9).

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141