Set accessor not setting value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arggg
    New Member
    • Mar 2008
    • 91

    Set accessor not setting value

    I have a statusbar and created a textbox to double test this however the value is being sent as I did a MessageBox.Show (value); and it shows the value being passed yet it is not being set. Can anyone explain why? Here is my code. The MainGUI is a partial class with : Form. Does that make a difference?

    MainGUI Class
    Code:
    public string stsMessageText
    {
    get
    {
    return stsMessage.Text;
    }
    set
    {
    stsMessage.Text = value;
    MessageBox.Show("Value: " + value);
    }
    }
    database.cs
    Code:
    MainGUI main = new MainGUI();
    main.stsMessageText = "Connected to database";
    Any help would be appreciated.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    How fast are you doing this? sometimes you need to call .Update() on the object to make sure it gets re-drawn?

    Comment

    • arggg
      New Member
      • Mar 2008
      • 91

      #3
      in Database.cs I now have
      Code:
      main.stsMessageText = "connected to database";
      main.Update();
      But this still does not make it display... any other ideas? Also thank you for the quick reply.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Hahaha I think I have it.
        You are creating a NEW instance of your maingui class in that database call, not changing the one that is already shown.

        Comment

        • arggg
          New Member
          • Mar 2008
          • 91

          #5
          Originally posted by Plater
          Hahaha I think I have it.
          You are creating a NEW instance of your maingui class in that database call, not changing the one that is already shown.
          Yea i was just going to post that. How would I update the CURRENT instance? I am not familiar with a way of doing something in another class without first creating an instance...

          Comment

          • Sick0Fant
            New Member
            • Feb 2008
            • 121

            #6
            Originally posted by arggg
            Yea i was just going to post that. How would I update the CURRENT instance? I am not familiar with a way of doing something in another class without first creating an instance...
            Instead of setting the object equal to a new instance via the keyword "new", just declare the variable and set it equal to an already existing instance.

            Comment

            • arggg
              New Member
              • Mar 2008
              • 91

              #7
              Originally posted by Sick0Fant
              Instead of setting the object equal to a new instance via the keyword "new", just declare the variable and set it equal to an already existing instance.

              Well I launched the application in the Main() with Application.Run (new MainGUI()); Well I didnt but VS2008 Express did. How should I proceed?

              Sorry I'm new to c#

              Comment

              • Sick0Fant
                New Member
                • Feb 2008
                • 121

                #8
                Originally posted by arggg
                Well I launched the application in the Main() with Application.Run (new MainGUI()); Well I didnt but VS2008 Express did. How should I proceed?

                Sorry I'm new to c#
                Is the form itself in which this code is contained an instance of MainGUI (if so, use this.stsMessage Text)? I'm kind of confused.

                Comment

                • arggg
                  New Member
                  • Mar 2008
                  • 91

                  #9
                  Here is what I did with the help of a colleage.

                  MainGUI.cs
                  Code:
                  public string stsMessageText
                  {
                  set { stsMessage.Text = value; }
                  }
                  
                  Database db = new Database();
                  db.dbConnect(this);
                  database.cs
                  Code:
                  class Database
                  
                  public void dbConnect(MainGUI main)
                  {
                  // ...
                  main.stsMessageText = "Connected To Database";
                  }
                  I believe that is the direction you were pointing me in or atleast trying to tell me to day. Thanks for the advice!

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    That should work, maybe a bit exsesive, but should work.

                    Comment

                    • arggg
                      New Member
                      • Mar 2008
                      • 91

                      #11
                      what were you thinking?

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        Well you wanted to use that get/set thing, but I was thinking you were passing in a reference to the whole form when all you needed was that one control

                        Comment

                        Working...