0

I have a Click function that starts up a program with the selected arguments

private void Launch_Click(object sender, RoutedEventArgs e)
 {
 //start up proccess here

 Process.StartInfo.Arguments = app + app_output + nosplash + showscripterrors;

 etc...

 }

works fine but for the string app_output is a foreach statement using the code below. The problem is that only the last foreach item is being set in the argument. I basically want the app_string to be itemA;itemB;itemC etc...

string app_string = "0";
string app_output = "0";
foreach (var item in TheList)
{
 //item.PropertyChanged += TheList_Item_PropertyChanged;
 if (item.IsSelected == true)
{
app_string = item.TheText;
app_output = app_string + ";";
}
else
{
//System.Windows.MessageBox.Show("no item selected");
}
}

how would i go about getting the app_output to render the foreach items? Shout i enter the foreach into an array?

Ive tried placing the foreach code within the argument string but does not allow foreach method inside it.

1
  • You are overwriting app_output with a new value in each iteration... app_output += app_string + ";" Commented Apr 17, 2015 at 9:28

2 Answers 2

3

use it:

string line = string.Join(";", TheList.Where(x => x.IsSelected).Select(x => x.TheText));

String.Join Method

Concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member.

https://msdn.microsoft.com/en-us/library/system.string.join%28v=vs.110%29.aspx

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

Comments

1

The best is using a StringBuilder:

StringBuilder sb = new StringBuilder();

        foreach(var item in TheList)
        {
            sb.AppendFormat("{0};",item);
        }

        string app_ouput = sb.ToString();

Comments

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.