Cannot access a non-static member

This is a discussion on Cannot access a non-static member within the C# Programming forums, part of the General Programming Boards category; hello, I'm trying to change a value of the textbox within a class, but I get an error: Cannot access ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    9

    Cannot access a non-static member

    hello,
    I'm trying to change a value of the textbox within a class, but I get an error:
    Cannot access a non-static member of outer type 'WindowsFormsApplication1.Form1' via nested type 'WindowsFormsApplication1.Form1.http'
    How do I access the textbox? Thanks!

    Code:
    namespace WindowsFormsApplication1
    {
    
      public partial class Form1 : Form
        {
    
            public class http
            {
                void update_text(string data)
                {
                   textBox1.Text = data;
                }
            }
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
        }
    }
    Last edited by codebot; 09-24-2010 at 06:08 PM.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    Your http class need access to a member of form1. Either pass a form1 instance to the constructor of your http class or think about your design. Why do you have an http class? What does it represent?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    Thanks nvoigt!
    I'm making a simple bot, that can login into a website and get my stats. The http deals with httpwebrequest. The reason I need this, is to being able display info into 'log' instead of using MessageBox

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    Ok, I can't seem to figure out how to put it together.

    Here is my code so far:
    Code:
    namespace WindowsFormsApplication1
    {
    
        public partial class Form1 : Form
        {
    
            public class http : Form
            {
    
    
    
    
                public void update_text()
                {
                    Form1 myForm = new Form1();
                    Application.Run(myForm);
                    myForm.textBox1.Text = "!@sdfdsf#";
                }
            }
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                http h = new http();
                h.update_text();
            }
        }
    }
    The text doesn't get updated. Why? Can you please give me a code example, I'm new to C#. Thanks!

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    Code:
    Application.Run(myForm);
    That's not what you want. You want
    Code:
    myForm.Show();

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    Thanks for the reply.
    When I change it to myForm.Show(); I get 30 instances of the form and all of them are updated with the text, except one. weird..

    notice the forms in the back have the text updated, the form in the front doesn't.
    Imageshack - weirdk.png

  7. #7
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Quote Originally Posted by codebot View Post
    Ok, I can't seem to figure out how to put it together.

    Here is my code so far:
    Code:
    namespace WindowsFormsApplication1
    {
    
        public partial class Form1 : Form
        {
    
            public class http : Form
            {
    
    
    
    
                public void update_text()
                {
                    Form1 myForm = new Form1();
                    Application.Run(myForm);
                    myForm.textBox1.Text = "!@sdfdsf#";
                }
            }
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                http h = new http();
                h.update_text();
            }
        }
    }
    The text doesn't get updated. Why? Can you please give me a code example, I'm new to C#. Thanks!
    Why are you creating a new instance of Form1 just to update the textbox????

    Why not pass your existing Form1 instance to the http class, for example:

    Code:
                public void update_text(Form1 obj)
                {
                    obj.textBox1.Text = "!@sdfdsf#";
                }
    and then...
    Code:
            private void Form1_Load(object sender, EventArgs e)
            {
                http h = new http();
                h.update_text(this);
            }

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    Thanks!
    I have two classes http and log class. The http class is going to handle the requests and the log class is going to store or display the information. I'm not going to call the log class directly, but instead let the http class deal with the log. Then I can in my Main call http.request("http://google.com"); and don't have to deal with anything else. Is it possible or is that a bad design? Thanks!

  9. #9
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Don't forget that a web request takes time to complete and therefore will freeze up your GUI until it has completed it's task. That is unless you perform the web request in a second thread. In which case you must allow for cross threading when you want to update the text in your textbox as ordinarily GUI components don't like to be touched from other threads. You can get around this by using textbox.BeginInvoke().

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by codebot View Post
    Thanks!
    I have two classes http and log class. The http class is going to handle the requests and the log class is going to store or display the information. I'm not going to call the log class directly, but instead let the http class deal with the log. Then I can in my Main call http.request("http://google.com"); and don't have to deal with anything else. Is it possible or is that a bad design? Thanks!
    Don't overcomplicate things. That is my advise. Use the default design you get in Visual Studio and try to integrate your http and log classes inside the Form1 class. My logic is working from where the default environment has stopped rather than redesigning the classes from the beginning even if it makes more sense. That way you keep separated your implementation and the default code given to you.

    For example: Create a new .cs file and implement an http class there. Don't bother on the "displaying" part. That class will get the results and store them in a List<string> or something else convinitent. Create the log class as well. Then in the main form you can have a button "Get Results" which would do something like this
    Code:
    private void GetResults_Click(object sender, EventArgs e)
    {
         http myHttp = new http("http://www.google.com");
         List<string> results =  myHttp.GetResults();
         log myLog = new Log();
         log.Append(results);
         resultBox = log.ToString(); //resultsBox being a TextBox to put the results
    }
    You could more simply have a GetResults() function that does what you want and not need two classes, but that is up to you. You would also probably want to instantiate the http and log classes outside the function, the above is just an example.

    To fix what theobe said you have another solution which I prefer.
    Run the GetResults() in a separate thread. You have separated your code from the displaying part so you can freely call another thread.
    When it is done it can set a flag. Then use a "wait code" like this
    Code:
    private void GetResults_Click(object sender, EventArgs e)
    {
         http myHttp = new http("http://www.google.com");
         List<string> results =  myHttp.GetResults();
         while(!myHttp.Done)
           Application.DoEvents();
         log myLog = new Log();
         log.Append(results);
         resultBox = log.ToString(); //resultsBox being a TextBox to put the results
    }
    the GetResults will set the Done flag.

    Finally, you might want to set one more flag not to get multiple presses of the Get Results button like
    Code:
    bool getting = false;
    private void GetResults_Click(object sender, EventArgs e)
    {
         if (getting) return;
         getting = true;
         http myHttp = new http("http://www.google.com");
         List<string> results =  myHttp.GetResults();
         while(!myHttp.Done)
           Application.DoEvents();
         log myLog = new Log();
         log.Append(results);
         resultBox = log.ToString(); //resultsBox being a TextBox to put the results
         getting = false;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 07:07 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. illegal references in static member functions
    By bennyandthejets in forum Windows Programming
    Replies: 10
    Last Post: 12-31-2002, 09:11 AM
  5. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21