Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
my code is

C#
string tabtype = "5,7,11";
string[] strTabTypes = tabtype.Split(',');
List<int> lstInt = new List<int>();

for (int i = 0; i < strTabTypes.Length; i++)
  lstInt.Add(Convert.ToInt32(strTabTypes[i]));


getting error when converting string to int

how to solve it
Posted
Updated 30-Oct-14 4:09am
v3
Comments
George Jonsson 30-Oct-14 9:50am    
What error do you get?
What is the contents of the string tabtype?
sanjaysgh 30-Oct-14 9:51am    
tabtype contain 5,7,11
Thomas Daniels 30-Oct-14 9:57am    
Are you sure that there are no spaces in tabtype?
BillWoodruff 30-Oct-14 10:26am    
The code the OP has posted (recently edited) will work even if there are Tabs, Spaces, or LineFeeds in the string.
sanjaysgh 30-Oct-14 10:00am    
yes i am sure no space is there

It's almost certainly a data problem, in that the contents of tabtype are not what you expected.
So try this:
C#
string[] strTabTypes = tabtype.Split(',');
List<int> lstInt = new List<int>();

for (int i = 0; i < strTabTypes.Length; i++)
   {
   try
      {
      lstInt.Add(Convert.ToInt32(strTabTypes[i]));
      }
   catch(Exception ex)
      {
      Console.WriteLine("Problem converting input {0} : \"{1}\" to an integer:\n{2}", i, strTabTypes[i], ex.Message);
      }
   }
It will at least tell you what the problem is.
 
Share this answer
 
C#
// using Linq; // required

string[] strTabTypes = tabtype.Split(',');

List<int> lstInt = strTabTypes.Select(str => Convert.ToInt32(str)).ToList();
Note that this approach is going to throw a System.FormatException error if any of the strings in the 'strTabTypes array are not valid for conversion to Int32.

Here's one way you could approach error-checking in the conversion using Linq:
C#
Int32 testInt;

List<int> lstIn2 = strTabTypes
    .Select(str => Int32.TryParse(str, out testInt) ? testInt : Int32.MaxValue)
    .ToList();
In this example, if there is any string that can't be converted to Int32, then Int32.MaxValue is returned: 2,147,483,647 : hexadecimal 0x7FFFFFFF.

But, perhaps the right thing for you to do is put the conversion in a Try/Catch block and handle the error, or re-throw the error ?
 
Share this answer
 
v2
The code below provides for a more robust solution compared to your original solution.

C#
string tabtype = "1,5,11"; // Just an example

// This way you make sure that there are no empty positions in the string array
string[] strTabTypes = tabtype.Split(new char[] { ',' },
                                      StringSplitOptions.RemoveEmptyEntries);
List<int> lstInt = new List<int>();

foreach (string s in strTabTypes)           // foreach is easier to use in this case
    lstInt.Add(Convert.ToInt32(s.Trim()));  // Removes head and tail spaces

You can of course add an error check according to solution 1.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900