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)"

8
  • What value should outrdr have? Commented Jul 7, 2016 at 6:54
  • what is the current value in AppSettings["OutReader"] Commented Jul 7, 2016 at 6:54
  • 1
    What is expected output? Commented Jul 7, 2016 at 6:56
  • AppSettings["OutReader"]="1,2" Commented Jul 7, 2016 at 6:57
  • Possible duplicate of Store String Array In appSettings? Commented Jul 7, 2016 at 6:57

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

@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.
@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.
string InReader = ConfigurationManager.AppSettings["InReader"].ToString(); string OutReader = ConfigurationManager.AppSettings["OutReader"].ToString(); directly it is working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.