Hi, now i do the sorted list for the combination i did. I want to read the data to the sorted list. But my foreach loop always had error. i fix it many times already but still can not solve it successfully. I will attach my coding. Hope someone can help me to solve the problem...

here is my coding. i will attach a full coding.

private void Browse_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog fileOpen = new OpenFileDialog())
            {

                try
                {
                    fileOpen.Filter = "All files (*.*)|*.*";
                    fileOpen.InitialDirectory = ".\\";
                    fileOpen.Title = "Open";

                    if (fileOpen.ShowDialog() == DialogResult.OK)
                    {
                        StreamReader sr = new StreamReader(fileOpen.FileName, Encoding.Default);
                        str = sr.ReadToEnd();
                        sr.Close();
                        ContentBox.Text = str;
                        filePath.Text = fileOpen.FileName;
                    }

                    SplitItems = (str).Split(new string[] { "\n\r\t", " " }, StringSplitOptions.None);

                    foreach (string listOut in SplitItems)
                    {
                        ListSplitItems += "\r\n" + listOut;
                    }

                    //txtCaseInputs.Text = ListSplitItems.Trim();


                }
                catch (Exception o)
                {
                    string Message = o.Message + "\n\nCannot open " + fileOpen.FileName;
                    MessageBox.Show(Message, "Open error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (File.Exists(fileOpen.FileName))
                {
                    StreamReader file = null;
                    try
                    {
                        file = new StreamReader(fileOpen.FileName);

                        while ((line = file.ReadLine()) != null)
                        {
                            //MessageBox.Show(line.ToString());
                            ListSplitLineByLine = "";
                            //String[] SplitItemLineByLine1;
                            //sb.AppendLine(line);
                            // txtCaseInputs.Text = sb.ToString().Trim();

                            //SplitItemLineByLine. = null;
                            line = line.Replace("\r\n"," ");
                            String[] SplitItemLineByLine1 = (line).Split(' ');

                            foreach (string lineByLine in SplitItemLineByLine1)
                            {
                                ListSplitLineByLine += "\r\n" + lineByLine;
                                s.AppendLine(ListSplitLineByLine);
                               
                            }
                            txtCaseInputs.Text = ListSplitLineByLine.ToString().Trim();

                            GenCombItems(); // 1-mac

                            //MessageBox.Show("re");
                            //txtCaseInputs.Clear();
                        }

                    }
                    catch (Exception o)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           {
                        string Message = o.Message + "\n\nCannot open " + fileOpen.FileName;
                        MessageBox.Show(Message, "Open error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }
        }
       
        private void GenCombItems()
        {
            //lbCombinations.Items.Clear();

            //yes

            //long n = int.Parse(txtTotalItems.Text);
            //long k = int.Parse(txtSubsetSize.Text);


            int n = txtCaseInputs.Lines.Length;

            //int k = int.Parse(txtSubsetSize.Text);
            int k = 2;

            txtNumCombinations.Text = Combination.Choose(n, k).ToString();

            Combination c = new Combination(n, k);

            string[] result = new string[k];

            while (c != null)
            {
                result = c.ApplyTo(txtCaseInputs.Lines);

                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < result.Length; ++i)
                {
                    sb.AppendFormat("{0} {1}", result[i], " ");
                }
               
                lbCombinations.Items.Add(sb.ToString());

         /*@@*/ foreach (String sort in sb.ToString().Trim())
                {          
                   if (!(sortList.ContainsKey(sort)){
                        sortList.Add(sort, 1);
                    }
                    else
                    {
                        int Count = (int)sortList[sort];
                        sortList[sort] = Count + 1;
                    }

                }
                c = c.Successor();               
            }
            txtCaseInputs.Clear();

           DisplayResult();// display result       
        }


        String ShowResult =" " ;
        private void DisplayResult()
        {
            
            IDictionaryEnumerator enumerator = sortList.GetEnumerator();

            while (enumerator.MoveNext())
            {
                ShowResult += "{"+enumerator.Key.ToString()+"}:" + enumerator.Value.ToString() + "\n";
                //ss.AppendLine(ShowResult);

            } 
           // MessageBox.Show(ss.ToString());
            listBox.Items.Add(ShowResult.ToString().Trim());
        }

        private void txtCaseInputs_TextChanged(object sender, EventArgs e)
        {     
            txtTotalItems.Text = (txtCaseInputs.Lines.Length).ToString();
        }

        private void txtSubsetSize_TextChanged(object sender, EventArgs e)
        {
            long n = int.Parse(txtTotalItems.Text);
            long k = int.Parse(txtSubsetSize.Text);
            txtNumCombinations.Text = Combination.Choose(n, k).ToString();
        }

       private void btnGenerateCombinations_Click(object sender, EventArgs e)
        {
            
            /* lbCombinations.Items.Clear();

            int n = txtCaseInputs.Lines.Length;
            int k = int.Parse(txtSubsetSize.Text);

            Combination c = new Combination(n, k);

            string[] result = new string[k];

            while (c != null)
            {
                result = c.ApplyTo(txtCaseInputs.Lines);

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < result.Length; ++i)
                {
                    sb.AppendFormat("{0} {1}", result[i], " ");
                }
                lbCombinations.Items.Add(sb.ToString());

                c = c.Successor();
            }*/
        }

        private void txtTotalItems_TextChanged(object sender, EventArgs e)
        {

        }

        private void tabPage6_Click(object sender, EventArgs e)
        {

        }

        private void filePath_TextChanged(object sender, EventArgs e)
        {

        }

        private void ContentBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void listBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

 }
    
}

The symbol /*@@*/ is the place that the error occur. Hope someone can help me to fix the error.

What error does it show?

Foreach is a loop. So you cannot assign it to read for one string.
You did like:

string a = "a";
foreach(string b in a){} //this is an error!

You have to do it like:

string[] array = {"a", "b"};
foreach(string b in array)
{
    //get each string seperated in here!
}

you can use an Array, a ListArray, a Genertic List - so something that has a collection! Not a string.
Hope it helps,
Mitja

commented: Great explanation and solve abilities! +8

To explain a little more, foreach expects the last argument (the one after the in) to have implemented the interface IEnumerable or IEnumberable<T>.

/*@@*/ foreach (String sort in sb.ToString().Trim())

This coding always give me error. the error is cannot convert type char to string.So, i don't know how to fix it. Because now the foreach loop need to read from the StringBuilder(sb.ToString().Trim()) to create sorted list.

eg.
the output i wan is like this
{1,2} :2
{1,3} :3
{2,3} :1
{2,4} :1

is it my foreach loop got problem..???

Since String does not implement IEnumerable the system is converting the String to a character array (which does implement IEnumerable). But the first part you want a string so you get the error (can't convert char to string).

I'm not sure what you are trying to do in that method (GenCombItems) at all. What code are you using for Combination? What is this code supposed to be doing (it seems like it's doing multiple things)? What is the meaning of the output you want generated (what does {1,2} : 2 mean?)?

i got attached the class.cs. GenComItems is used to do the Combination. {1,2} : 2
this means combination for {1 ,2} is appear in two times.

let say

1 2 3 4
3 4 5

{1 2}:1
{1 3}:1
{1 4}:1
{2 3}:1
{3 4}:2
{3 5}:1
{4 5}:1
The {3 4} appear two times..

So now my sb.ToString().Trim() is in string but foreach loop is use in Char. So how i convert the sb.ToString().Trim() to char. So, the sb.ToString().Trim() can use in foreach loop?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.