• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

Static TextBox Error?

I keep recieving and error because of this block of code:

       "An object reference is required for the nonstatic field, method, or property"

[Code]
private static void ProcessDone(object sender, EventArgs e)
{    
    textBox1 = bool_Result.ToString();
}

Help....
0
kvnsdr
Asked:
kvnsdr
  • 8
  • 6
1 Solution
 
JimBrandleyCommented:
I think what you need is:
textBox1.Text = bool_Result.ToString();
0
 
JimBrandleyCommented:
The previous assumes textBox1 is a TextBox control that is in scope for this method.
It's also not clear where bool_result comes from. Is that a member variable of this class?
0
 
kvnsdrAuthor Commented:
Sorry, typo.

Here's the first part of the code that 'calls' the ProcessDone method.

Again, the errror is:

"An object reference is required for the nonstatic field, method, or property"

[Code]
private TextBox textBox1;

private void RunProcess
{
Process proc = new Process();
                proc.StartInfo.FileName = @"C:\checkFileExits.exe";
                proc.EnableRaisingEvents = true;
                proc.Exited += new EventHandler(ProcessDone);
                proc.Start();
                proc.WaitForExit();
}

private static void ProcessDone(object sender, EventArgs e)
{    
    textBox1.Text = bool_Result.ToString();
}

Help....

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JimBrandleyCommented:
I do not see any definition for bool_Result. The rest of it looks valid.

Also, the compiler usually shows the error message in the ErrorList panel at the bottom of the screen. If you click on that error, it will take you to the line of code it did not like. I also underlines the bit that made it unhappy. If it is not bool_Result, which one is it?
0
 
kvnsdrAuthor Commented:
textBox1.Text
0
 
JimBrandleyCommented:
Oops - Change
private TextBox textBox1;
to
private TextBox textBox1 = new TextBox();
0
 
JimBrandleyCommented:
In general, when you define a local member, it's a good idea to initialize it to something, even if it's just null. At that point, you have created a name for an object of a specific kind. You can use that name later in your code. However, that name does not yet have anything behind it is it's a reference type. So the first thing you have to do with it is:
1. Accept a reference to an object of that type from some caller outside your code and assign that object to your reference (the name you defined earlier), or
2. Invoke the appropraite constructor using the new operator to create an object of that type and assign it to the reference (really a pointer) you created earlier.

0
 
kvnsdrAuthor Commented:
Still nothing, even tried this:

private TextBox textBox1 = new TextBox;

private static void ProcessDone(object sender, EventArgs e)
{    
    TextBox textBox1 = null;
    textBox1.Text = bool_Result.ToString();
}

0
 
kvnsdrAuthor Commented:
I should note that it dosen't error, but nothing shows in the textbox either.......
0
 
JimBrandleyCommented:
Where is bool_Result defined? I have not seen it yet.
Also, you only want to set some member to null BEFORE you initialize it, so change to:
private static void ProcessDone(object sender, EventArgs e)
{    
    textBox1.Text = bool_Result.ToString();
}
0
 
kvnsdrAuthor Commented:
using .....

namespace...

    public class Form1 : System.Windows.Forms.Form
    {
           internal static bool bool_Result = false;
           private TextBox textBox1;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
        }

Note:  I had to make the bool_Result static to get ProcessDone to accept it.
I wish it where that easy for the Textbox.
0
 
JimBrandleyCommented:
Providing bool_result is defined in the same class that's making the reference, it should not have to be static. For example:
private static bool bool_result = false;

Then somewhere else in the code,
if (what I did was OK)
   bool_Result = true;

Then when you want to use it, you should get the last value to which it was set with no trouble.
0
 
JimBrandleyCommented:
Sorry - I meant to drop the static modifier from the definition in the previous post.
0
 
kvnsdrAuthor Commented:
I'll work on some changes and get back to you.....

Thanks.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now