473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

C# Windows App - "Global" shared variable

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
Expand|Select|Wrap|Line Numbers
  1. namespace CustomerApp
  2.  
  3. {
  4.  
  5.      class Cust_Vals
  6.  
  7.      {
  8.  
  9.          public static class ActiveID
  10.  
  11.          {
  12.  
  13.                  private static string str_aid = "";
  14.  
  15.  
  16.  
  17.                  public static string aid
  18.  
  19.                  {
  20.  
  21.                      get { return str_aid; }
  22.  
  23.                      set { str_aid = value; }
  24.  
  25.                  }
  26.  
  27.          }
  28.  
  29.         public string aid = "";
  30.  
  31.  
  32.  
  33.         public static class ActiveName
  34.  
  35.         {
  36.  
  37.             private static string str_aname = "";
  38.  
  39.  
  40.  
  41.             public static string aname
  42.  
  43.             {
  44.  
  45.                  get { return str_aname; }
  46.  
  47.                  set { str_aname = value; }
  48.  
  49.             }
  50.  
  51.         }
  52.  
  53.         public string aname = "";
  54.  
  55.     }
  56.  
  57. }
  58.  

I set it like this in one form:
Expand|Select|Wrap|Line Numbers
  1. Cust_Vals cv = new Cust_Vals();
  2.  
  3. cv.aname = "x";
  4.  
  5. cv.aid = "12345";
  6.  

Then later try to retrieve it like this from a different form:
Expand|Select|Wrap|Line Numbers
  1. Cust_Vals cv = new Cust_Vals();
  2.  
  3. lblCustomerName.Text = cv.aname;
  4.  
  5. lblCustomerID.Text = cv.aid;
  6.  

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 ...


Expand|Select|Wrap|Line Numbers
  1. public void ActivateCustomer(string Cust, string ID)
  2.  
  3. {
  4.  
  5.      lblCustomerName.Text = Cust; 
  6.  
  7.      lblCustomerID.Text = ID;
  8.  
  9.      this.Refresh();
  10.  
  11. }
  12.  
  13.  
I call it from a different form like this:
Expand|Select|Wrap|Line Numbers
  1. frmMain frm = new frmMain();
  2.  
  3. frm.ActivateCustomer("x", "1");
  4.  

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!
May 5 '08 #1
1 3662
Plater
7,872 Expert 4TB
I like to create a public static class (no constructors) to hold various data (and functions) for my projects.

For example:
Expand|Select|Wrap|Line Numbers
  1. public static class GlobalVars
  2. {
  3.    public static int ClientID=-1;//start out with "no" clientID
  4.    public static string ClientName=""//start out empty
  5.  
  6. }
  7.  
You would reference that anywhere you wanted with:
Expand|Select|Wrap|Line Numbers
  1. GlobalVars.ClientName="Jan's Hotdog Stand";
  2.  
or
Expand|Select|Wrap|Line Numbers
  1. string myClientName=GlobalVars.ClientName;
  2.  

Now you can expand upon this idea and create a static class for all your Client-related functions.

I have a static class called "DBFunctions" in many projects that contains any/all functions that make calls to my database. (that's just an example)
May 6 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: mrbog | last post by:
I have an array/hash that stores path information for my app. As in, what directory this is in, what directory that's in, what the name of the site is, what the products are called, etc. It's...
7
by: Lyn | last post by:
Hi and Season's Greetings to all. I have a question regarding the use of a qualifier word "Global". I cannot find any reference to this in Access help, nor in books or on the Internet. "Global"...
8
by: Mike Turco | last post by:
This is a strange one. I have an app that needs to know who is using the program but there's no need for security. When the program opens a form comes up with a listbox. The user double-clicks...
5
by: j | last post by:
Anyone here feel that "global variables" is misleading for variables whose scope is file scope? "global" seems to imply global visibility, while this isn't true for variables whose scope is file...
13
by: Le, Thanh-Nhan | last post by:
Hi, How can I define a global variable in .NET? I mean, everywhere in the same project can access this variable. Thanks Nhan
4
by: BB | last post by:
Hello all, I might be missing something here, but am trying to understand the difference between using application-level variables--i.e. Application("MyVar")--and global variables--i.e. public...
1
by: Imran Aziz | last post by:
Hello All, How can I create a Global shared function that automatically gets shared in all ASP.net files. The issue is that when a user comes to the site I am creating, I check if the user is...
4
by: jubelbrus | last post by:
I need to initiate a global variable to a class before the initialization of any other global variable. The problem is that when I link the application with a dll, some or all global variables in...
2
by: Sir Psycho | last post by:
Is there a way to set a global variable without using the Session object? I basically want to be able to track if a visitor is logged in without starting a Session. Is there such a thing as a...
4
ChrisWang
by: ChrisWang | last post by:
Hi, I am having trouble understanding the use of 'global' variables I want to use a global variable with the same name as a parameter of a function. But I don't know how to use them at the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.