473,385 Members | 1,834 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,385 software developers and data experts.

declare a global variable

Need to know, how can i declare a variable as global
in a form and if i can use it in another.

(I hope this isn't a vague question)


Thanks and regards
Feb 22 '11 #1
2 2582
Leito
58
Just declare it as public in your form Form1, then refer to it as Form1.variable in your second form.
Feb 22 '11 #2
GaryTexmo
1,501 Expert 1GB
Generally you should try to avoid using globals; however, it's still sometimes appropriate to do so. In these cases, I'd recommend making yourself a static class called Globals, with public static members.

Expand|Select|Wrap|Line Numbers
  1. namespace YourAppName.Global
  2. {
  3.   public static class Globals
  4.   {
  5.     public float PI = 3.141592654;
  6.     public string VERSION = "v1.54";
  7.     ...
  8.   }
  9. }
Now, if you include the namespace (and a reference, where appropriate), you can access it anywhere in your code using...

Expand|Select|Wrap|Line Numbers
  1. Console.WriteLine(Globals.PI);
  2. Console.WriteLine(Globals.VERSION);
I hope that helps!
Feb 22 '11 #3

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

Similar topics

2
by: francisl | last post by:
I have a problem when I declare global variable. If it is string, dict, array or tuple, everything goes fine, but with int, I get an "used before declaration" error. here a sample : vardict =...
4
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
6
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the...
5
by: luc wastiaux | last post by:
Hello, I need to share a global variable between functions, and for clarity reasons, my code is split in different .c files that each compile into a ..o (object) which in turn are compiled...
8
by: chellappa | last post by:
hi Everybody ! decalaring variable in a.h and using that vaaariable in a1.c and inalization is in main.c it is possible .........pleaase correct that error is it Possible? i am trying it...
41
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query =...
2
by: mars | last post by:
In my application, global variable A, during its construction, depends on another global variable, say B. However, variable B may have not been initialized by then. How can I make sure that...
1
by: Gandham | last post by:
hi i am sujatha how to declare global varibles in .net 2005
16
by: aeo3 | last post by:
I need to define a global variable that many files will see it, this variable is a pointer to class. but i failed to do it. I have a set of classes each in separated files
1
by: lokesh kumar | last post by:
where and how to declare global variable in vb2005 ? EggHeadCafe - .NET Developer Portal of Choice http://www.eggheadcafe.com
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.