Question

So far if a user inputs something, I store in a label property. I know this can't be right. How can I update a variable based on user input for use across whichever event needs to use it?

This is one of many things I've tried. I can't even figure out the right search terms to google the solution for what I need to do.

namespace Words
{
  public partial class formWords : Form
  {
    int x = 5;
    int y = 50;
    int buttonWidth = 120;
    int buttonHeight = 40;
    string fileList = "";
    string word = "";
    string wordFolderPath = @"C:\words\";// this is the variable I want to change with the dialog box below.

  private void selectWordFolderToolStripMenuItem_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog folder = new FolderBrowserDialog();
        if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string folderPath = folder.SelectedPath;
            formWords.wordFolderPath = folderPath;
        }
    }
Was it helpful?

Solution

wordFolderPath is a variable public to your class (but private outside of it). This means that anything inside your class can freely read/write the value.

As for your syntax, you can just use the variable name or use this.:

private void DoAThing()
{
    wordFolderPath = "asdf";
    this.wordFolderPath = "qwerty"; //these are the same
}

You can't use the current class's name when accessing an internal variable. formWords is a type, not an instance.

The only advantage of using this is because it is legal to have a variable of the same name defined within your method. Using this keyword makes sure you are talking about the class's member.

OTHER TIPS

Just changing formWords.wordFolderPath = folderPath;

to wordFolderPath = folderPath;

or this.wordFolderPath = folderPath;

should fix your problem

Also, there should have been a compiler error in the error list saying "An object reference is required for the non-static field, method, or property..."

If you don't have your error list visible you should definitely turn it on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top