Scott

Scott

  • NA
  • 1
  • 0

C# Windows App - "Global" shared variable

May 5 2008 5:55 PM

This should be kindergarten stuff but for some reason I am having trouble with it.  I do 99.9% web programming and this is the first windows app i've done in years - probably the first ever in C# ... anyway ...

 

Here is what I am trying to do ....  Windows app - with a main "navigation" form with 4 linkbuttons on it - each of them open a child form with a task to perform ... on the main form, there is a Label control for the "Active Customer" ... meaning their name and customer ID to display - showing the user which customer they are working with ....  It is a sequential process - so when the user opens child form 1 - they fill out a small info page, then a customer ID is generated and I want to be able to store the customer's name in a variable and their customer id in another variable - and be able to access those values from every form in the application until either a new customer is generated overwriting the values - or the user closes the application then it can be lost in memory - or i guess ideally it would be nice to store that somewhere on the user's pc to pull up later - but that's beyond what's needed i guess ...

 

I am OK with either of these solutions -

A) when the ID is generated, populate the Label controls on the main form for Customer Name and Customer ID, which I can then reference throughout the life of the application from all other forms

B) store the 2 values in "global variables" which everything within the application can read/write to when needed

 

Here is what I first came up with - but for some reason I cannot get it to:

A) keep the stored value once the form that populates it is closed

B) get the main form to refresh itself and change the Label control values with these values - even when sent directly to it

 

Attempt 1:

I put this in a .cs file in the project

namespace CustomerApp

{

     class Cust_Vals

     {

         public static class ActiveID

         {

                 private static string str_aid = "";

                

                 public static string aid

                 {

                     get { return str_aid; }

                     set { str_aid = value; }

                 }

         }

        public string aid = "";

 

        public static class ActiveName

        {

            private static string str_aname = "";

          

            public static string aname

            {

                 get { return str_aname; }

                 set { str_aname = value; }

            }

        }

        public string aname = "";

    }

}

 

I set it like this in one form:

Cust_Vals cv = new Cust_Vals();

cv.aname = "x";

cv.aid = "12345";

 

Then later try to retrieve it like this from a different form:

Cust_Vals cv = new Cust_Vals();

lblCustomerName.Text = cv.aname;

lblCustomerID.Text = cv.aid;

 

When I do so, the values come back blank ... again, I'm sure I'm just doing something incorrectly - it's been a while since I've worked with windows forms and applications ...

 

Attempt 2:

I created a method in my main form to populate the Labels and allow the 2 values to be passed in, then made it public so I can call it from another form ... this works fine, I can step through and see the values come in correctly - but my main form never changes the values visually - I've tried doing a .Refresh() or .Invalidate() .... even tried hiding the form, calling the function, then showing the form with a .Refresh() attached and still - blank Label controls ...

 

public void ActivateCustomer(string Cust, string ID)

{

     lblCustomerName.Text = Cust;

     lblCustomerID.Text = ID;

     this.Refresh();

}

 

I call it from a different form like this:

frmMain frm = new frmMain();

frm.ActivateCustomer("x", "1");

 

The values pass correctly, but the Labels on the form never update ....

 

Any help would be greatly appreciated - thanks so much - I know this is probably something stupid!

 


Answers (2)