How do I fix the error produced when using the HashSet method

Can someone help me out on this code generated in Copilot for finding a certain word in a rich text box in Visual code studio Forms design, and filling a combo box with all the words that contain a T and the 4 digits after that. This is the code that was generated but it produces errors at .Add and . Contains.

:

using System.Text.RegularExpressions;

private void FillToolList()

{

    // Clear the ComboBox before adding new items

    CmbxToolList.Items.Clear();

    // Regex pattern: T followed by exactly 4 digits

    Regex rx = new Regex(@”\bT\d{4}\b”, RegexOptions.IgnoreCase);

    // HashSet ensures we only add unique values

    HashSet found = new HashSet();

    // Loop through each line in the RichTextBox

    foreach (string line in RtbFileToSend.Lines)

    {

        // Get all matches in the line

        MatchCollection matches = rx.Matches(line);

        foreach (Match m in matches)

        {

            // Add only unique tool codes

            if (!found.Contains(m.Value))

            {

                found.Add(m.Value);

                CmbxToolList.Items.Add(m.Value);

            }

        }

    }

}

Read more here: Source link

How do I fix the error produced when using the HashSet method

Can someone help me out on this code generated in Copilot for finding a certain word in a rich text box in Visual code studio Forms design, and filling a combo box with all the words that contain a T and the 4 digits after that. This is the code that was generated but it produces errors at .Add and . Contains.

:

using System.Text.RegularExpressions;

private void FillToolList()

{

    // Clear the ComboBox before adding new items

    CmbxToolList.Items.Clear();

    // Regex pattern: T followed by exactly 4 digits

    Regex rx = new Regex(@”\bT\d{4}\b”, RegexOptions.IgnoreCase);

    // HashSet ensures we only add unique values

    HashSet found = new HashSet();

    // Loop through each line in the RichTextBox

    foreach (string line in RtbFileToSend.Lines)

    {

        // Get all matches in the line

        MatchCollection matches = rx.Matches(line);

        foreach (Match m in matches)

        {

            // Add only unique tool codes

            if (!found.Contains(m.Value))

            {

                found.Add(m.Value);

                CmbxToolList.Items.Add(m.Value);

            }

        }

    }

}

Read more here: Source link