'How to get a textfield as a string[]

so I am writing a c# program which will return a text file selected in a textbox a string[].

But when trying to do this it gets an error saying "a static local function cannot contain a reference to this or base".

Here is the code for collecting the textbox string

         string[] words = { File.ReadAllText(dicuploadname.Text)

This is where the error appears. The code for the textbox in question is as follows:

Private void uploaddict_Click(object sender, EventArgs e)

    {

        OpenFileDialog fdlg = new OpenFileDialog();

        MessageBox.Show("The dictionary will need to be a .txt file.");

        fdlg.Title = "Select your dictionary file";

        fdlg.InitialDirectory = @"c:\";

        fdlg.Filter = "Text files (*.txt)|*.txt";

        fdlg.FilterIndex = 2;

        fdlg.RestoreDirectory = true;

        if (fdlg.ShowDialog() == DialogResult.OK)

        {

            dicuploadname.Text = fdlg.FileName;

        }

    }



   



    public static void dicuploadname_TextChanged(object sender, EventArgs e)

    {



    }

    public string text = dicuploadname.Text;

}

}

Also included is the code for the browse button that selects the text file and pastes it's location to the textbox.

Not sure how to return the contents of the text file as a string[]? Thanks.

Newest code:

else
        {
            var myInt = int.Parse(min.Text);
            int seconds = myInt * 60000;

            System.Timers.Timer aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new ElapsedEventHandler(TimerMethod);
            aTimer.Interval = seconds;
            aTimer.Enabled = true;
            // Do something with t...
            string uriString = "https://open.spotify.com/search/";

            static void TimerMethod(object source, ElapsedEventArgs e)
            {
                string uriString = "https://open.spotify.com/search/";



                WebClient webClient = new WebClient();

                Random r = new Random();

                string words;

                words = File.ReadAllText(dicuploadname.Text);

                Console.WriteLine(words[r.Next(0, words.Length)]);


                string word = words[r.Next(0, words.Length)];

                NameValueCollection nameValueCollection = new NameValueCollection();
                nameValueCollection.Add(uriString, word);

                webClient.QueryString.Add(nameValueCollection);

                var spotifysearch = new ProcessStartInfo
                {
                    FileName = "https://open.spotify.com/search/" + word,
                    UseShellExecute = true
                };
                Process.Start(spotifysearch);
            }
        }
    }

    private void HiveMind_Click_1(object sender, EventArgs e)
    {
   
        var HiveMind= new HiveMind();
        HiveMind.Show();
    }

    private void uploaddict_Click(object sender, EventArgs e)
    {
        string[] words;
        OpenFileDialog fdlg = new OpenFileDialog();
        fdlg.Title = "Select your dictionary file";
        fdlg.InitialDirectory = @"c:\";
        fdlg.Filter = "Text files (*.txt)|*.txt";
        fdlg.RestoreDirectory = true;

        if (fdlg.ShowDialog() == DialogResult.OK)
            words = File.ReadAllText(fdlg.FileName).Split(' ');
    }
    

    public string words;

    private void dicuploadname_TextChanged(object sender, EventArgs e)
    {

    }

}
}


Solution 1:[1]

Looking at your posted code, dicuploadname is not a variable but a event handler. Change it to this, which assigns the variable words by reading the user specified text file and splitting it on the space character.

private void uploaddict_Click(object sender, EventArgs e)
    {
        string[] words;
        OpenFileDialog fdlg = new OpenFileDialog();
        fdlg.Title = "Select your dictionary file";
        fdlg.InitialDirectory = @"c:\";
        fdlg.Filter = "Text files (*.txt)|*.txt";
        fdlg.RestoreDirectory = true;

        if (fdlg.ShowDialog() == DialogResult.OK)
            words = File.ReadAllText(fdlg.FileName).Split(' ');
    }

In this code the file the user picks is split on the space character. If you just wanted a string of the code and not each word change the declaration of words to

string words;

and the line where the variable gets assigned to

words = File.ReadAllText(fdlg.Filename);

hope this helps

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MtnManChris