Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Playing around, trying to figure out C#. The allFiveLetterWords list is either cleared by the time the possibilitiesTextBox_TextChanged event happens, or it is creating a temporary list with nothing in it at that point, because nothing is there at that point even though there was at the end of the Form1_Load event.

namespace wordleSolver
{
    public partial class Form1 : Form
    {
        public List<string> allWords = new List<string>();
        public List<string> allFiveLetterWords = new List<string>();
        public List<string> currentPossibilities = new List<string>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string path = "D:\\Programs\\MyProgs\\wordleSolver\\words.txt";
            System.IO.StreamReader sr = new System.IO.StreamReader(path);

            while (!sr.EndOfStream)
            {
                string str = sr.ReadLine();
                allWords.Add(str);
            }
            sr.Close();

            foreach (string str in allWords)
            {
                if (str.Length == 5) allFiveLetterWords.Add(str);
            }
            currentPossibilities = allFiveLetterWords;
            foreach (string str in currentPossibilities)
            {
                possibilitiesListBox.Items.Add(str);
            }
        }



        private void possibilitiesTextBox_TextChanged(object sender, EventArgs e)
        {
            currentPossibilities.Clear();

            string possibleChars = possibilitiesTextBox.Text;

            int possibleCharsLen = possibleChars.Length;

            //THERE AIN'T NOTHING IN 'allFiveLetterWords' HERE!!!!!!!
            foreach (string str in allFiveLetterWords)
            {
                for (int i=0; i<5; ++i)
                {
                    for (int j=0; j<possibleCharsLen; ++j)
                    {
                        if (allFiveLetterWords[i][j] == possibleChars[j])
                        {
                            currentPossibilities.Add(str);
                            goto breakout;
                        }
                    }
            breakout:;
                }
            }
            possibilitiesListBox.Items.Clear();
            foreach (string str in currentPossibilities) {
                possibilitiesListBox.Items.Add(str);    
            }

        }
    }
}


What I have tried:

Swearing, more cursing, all the usual suspects.
Posted
Updated 7-Sep-22 22:36pm

Swearing and cursing may help you feel better about things, but they don't help fix software problems. What does help is the debugger. Assuming the file has lines, then allWords will contain entries.
If allWords has entries, they will be added to allFiveLetterWords only if the string length is five.

So if allFiveLetterWords is empty after the loop to fill it, I'd suspect the data source file isn't "clean" - I'd use the debugger to look at exactly what allWords contains, and check for any whitespace or punctuation which might be difficult to see (or invisible) but make the length of a word 6 or 7 with only 5 alpha characters.

Sorry, but we can't do any of that for you!
 
Share this answer
 
Comments
David O'Neil 8-Sep-22 4:13am    
The 'allFiveLetterWords' contains 22,950 items at the end of the Form1_Load event. Thereby the swearing and cursing. I don't understand why it would empty itself out.
I am not a C# expert, but it looks there is a suspicious reference assigment:
In Form1_Load
Quote:
currentPossibilities = allFiveLetterWords;
Then, in possibilitiesTextBox_TextChanged.
Quote:
currentPossibilities.Clear();
I guess this magically clears allFiveLetterWords.

Replace (in Form1_Load)
Quote:
currentPossibilities = allFiveLetterWords;
foreach (string str in currentPossibilities)
{
possibilitiesListBox.Items.Add(str);
}

With
C#
foreach (string str in allFiveLetterWords)
{
    possibilitiesListBox.Items.Add(str);
}
 
Share this answer
 
v2
Comments
David O'Neil 8-Sep-22 4:43am    
Thank you.
CPallini 8-Sep-22 4:59am    
You are welcome.
Your problem is with this line:
C#
currentPossibilities = allFiveLetterWords;

A list is allocated on the heap when you call new List<string>() and the address of that list is stored in the variable. When you access the variable (ie. calling .Add()) you're asking C# to find the list in the heap, then call the Add() method.

With the above, what you've done is said "the currentPossibilities variable should point to the same list as allFiveLetterWords". Then, in your text changed event you call:
C#
currentPossibilities.Clear();

Which also says "clear all values out of the list at the address of currentPosibilities", which also happens to be the same address as allFiveLetterWords. You can fix this easily by simply creating a new list:
C#
currentPossibilities = new List<string>(allFiveLetterWords);

This page has a fairly decent explanation of heap vs. stack[^]
 
Share this answer
 
Comments
David O'Neil 8-Sep-22 4:43am    
Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900