C# - simple problem with methods and classes

Discussion in 'OT Technology' started by KingNothing, Jun 5, 2003.

  1. KingNothing

    KingNothing New Member

    Joined:
    Mar 4, 2002
    Messages:
    11,729
    Likes Received:
    0
    Location:
    Atlanta & Auburn
    I have two classes:

    • Form1.cs
      server.cs

    Form1.cs contains info about the GUI, and, in particular, a text box named textBox1.

    I need to somehow be able to call Form1.textBox1.Text from server.cs.

    I tried creating a static method in Form1.cs that took a string as a parameter, but I received a compile error saying that basically I could not access non-static data from a static method.

    How do I go about doing that?
     
  2. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    When you call the method in server.cs, have it return a string. Whatever is returned can then be set as the text for your textbox in Form1.cs. ;)
     
  3. SLED

    SLED custom title

    Joined:
    Sep 20, 2001
    Messages:
    28,123
    Likes Received:
    12
    or you can change the modifier of the textbox from "private" to "public". Then you can call the name of your class Form1.textBox1.Text = "blah blah";
     
  4. KingNothing

    KingNothing New Member

    Joined:
    Mar 4, 2002
    Messages:
    11,729
    Likes Received:
    0
    Location:
    Atlanta & Auburn
    Oh, of course. Thanks. :)
     
  5. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    You really should keep your local variables private though.
     
  6. KingNothing

    KingNothing New Member

    Joined:
    Mar 4, 2002
    Messages:
    11,729
    Likes Received:
    0
    Location:
    Atlanta & Auburn
    Ok, I just realized why having the method in server.cs return a value won't work...

    I need to be able to run the method in server.cs and cause the text box in form1.cs to update when it is run.
     
  7. SLED

    SLED custom title

    Joined:
    Sep 20, 2001
    Messages:
    28,123
    Likes Received:
    12
    :mb: :big grin:
     
  8. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    :rofl:

    You know it's true. :o
     

Share This Page