0

how to store multiple string value in one int variable

string OutReader = ConfigurationManager.AppSettings["OutReader"].ToString();
int outrdr = Convert.ToInt32(OutReader);

The value of AppSettings["OutReader"] is: "(1,2)"

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Jay Tambe JR
  • 9
  • 1
  • 1
  • 7

1 Answers1

1

If AppSettings["OutReader"] currently have in it a string like: "(1,2)" then you can do:

var sections = ConfigurationManager.AppSettings["OutReader"].Replace("(",string.Empty)
                        .Replace(")",string.Empty)
                        .Split(',');
if(sections.Length > 0)
{
    int outrdr = Convert.ToInt32(sections[0]);
}

This can still throw an exception in the case that section[0] can't be parsed into an int so use .TryParse instead - Just wanted to stay as close to the question as possible

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • @Jay - please explain again what is currently not working. And when you do, please edit your original question (not adding a comment) with the current code and error you receive. – Gilad Green Jul 08 '16 at 07:46
  • @Jay - the result is that `outrdr` contains the value `1` (starting from `(1,2)`). It is not possible to store the `2` as well in that single integer, but you can use `sections[1]` to access that using similar code. – Hans Kesting Jul 08 '16 at 07:58
  • string InReader = ConfigurationManager.AppSettings["InReader"].ToString(); string OutReader = ConfigurationManager.AppSettings["OutReader"].ToString(); directly it is working – Jay Tambe JR Jul 08 '16 at 08:01