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

Global variables in C#

Is it possible to create a 'global variable' in C# that can be used and
accessed between forms? I've used MODULES in Visual Basic, but can't find the
equivalent for C# ...

Thanks to all those responding,
Aug 30 '07 #1
18 5718
Are you looking for static variables?

-James

Aug 30 '07 #2
Yes - variables that can be manipulated inside of any form that the user
touches during the application.
"james" wrote:
Are you looking for static variables?

-James

Aug 30 '07 #3
Sam
I don't think there is something equivalent in C#, you might want to use
static class which kinda does the same thing for you.

Sam

"vbtrying" <vb******@discussions.microsoft.comwrote in message
news:DB**********************************@microsof t.com...
Is it possible to create a 'global variable' in C# that can be used and
accessed between forms? I've used MODULES in Visual Basic, but can't find
the
equivalent for C# ...

Thanks to all those responding,

Aug 30 '07 #4
On Aug 30, 2:10 pm, vbtrying <vbtry...@discussions.microsoft.com>
wrote:
Is it possible to create a 'global variable' in C# that can be used and
accessed between forms? I've used MODULES in Visual Basic, but can't find the
equivalent for C# ...

Thanks to all those responding,
Just add a public static variable to any class.

Aug 30 '07 #5
Thanks Sam ...

Question: Would that be a PUBLIC statis class, so as to be available to all
forms?

Thanks,
"Sam" wrote:
I don't think there is something equivalent in C#, you might want to use
static class which kinda does the same thing for you.

Sam

"vbtrying" <vb******@discussions.microsoft.comwrote in message
news:DB**********************************@microsof t.com...
Is it possible to create a 'global variable' in C# that can be used and
accessed between forms? I've used MODULES in Visual Basic, but can't find
the
equivalent for C# ...

Thanks to all those responding,


Aug 30 '07 #6
Thanks Brian ...

"Brian Gideon" wrote:
On Aug 30, 2:10 pm, vbtrying <vbtry...@discussions.microsoft.com>
wrote:
Is it possible to create a 'global variable' in C# that can be used and
accessed between forms? I've used MODULES in Visual Basic, but can't find the
equivalent for C# ...

Thanks to all those responding,

Just add a public static variable to any class.

Aug 30 '07 #7
Sam
Yes, its scope has to be public to be visible to all forms, and as well as
to others classes.

Sam
"vbtrying" <vb******@discussions.microsoft.comwrote in message
news:AF**********************************@microsof t.com...
Thanks Sam ...

Question: Would that be a PUBLIC statis class, so as to be available to
all
forms?

Thanks,
"Sam" wrote:
>I don't think there is something equivalent in C#, you might want to use
static class which kinda does the same thing for you.

Sam

"vbtrying" <vb******@discussions.microsoft.comwrote in message
news:DB**********************************@microso ft.com...
Is it possible to create a 'global variable' in C# that can be used and
accessed between forms? I've used MODULES in Visual Basic, but can't
find
the
equivalent for C# ...

Thanks to all those responding,



Aug 30 '07 #8
Perfect ... thanks Sam ...
"Sam" wrote:
Yes, its scope has to be public to be visible to all forms, and as well as
to others classes.

Sam
"vbtrying" <vb******@discussions.microsoft.comwrote in message
news:AF**********************************@microsof t.com...
Thanks Sam ...

Question: Would that be a PUBLIC statis class, so as to be available to
all
forms?

Thanks,
"Sam" wrote:
I don't think there is something equivalent in C#, you might want to use
static class which kinda does the same thing for you.

Sam

"vbtrying" <vb******@discussions.microsoft.comwrote in message
news:DB**********************************@microsof t.com...
Is it possible to create a 'global variable' in C# that can be used and
accessed between forms? I've used MODULES in Visual Basic, but can't
find
the
equivalent for C# ...

Thanks to all those responding,


Aug 30 '07 #9
Actually, the class doesn't necessarily have to be static, though static
does fit into the picture at some point, I suppose.

By default, C# (and I use VS.NET 2003, so I'm still in the dark ages),
creates a windows form app with the app main() startup in the same file as
the initial form.

What I normally do is break this out into a separate class called MainApp or
something like this. I then create an instance of my MainApp and have a
public static MainApp.App field to get to the instance from any class.

Then I can store all sorts of global goodies in my MainApp and access it
from any class. I find this to be very handy for doing things having an
application preferences which can be loaded on startup and saved on
shutdown, and what have you.


"Sam" <as****@yahoo.comwrote in message
news:u9**************@TK2MSFTNGP03.phx.gbl...
>I don't think there is something equivalent in C#, you might want to use
static class which kinda does the same thing for you.

Sam

"vbtrying" <vb******@discussions.microsoft.comwrote in message
news:DB**********************************@microsof t.com...
>Is it possible to create a 'global variable' in C# that can be used and
accessed between forms? I've used MODULES in Visual Basic, but can't find
the
equivalent for C# ...

Thanks to all those responding,


Aug 30 '07 #10
On Aug 30, 3:36 pm, "pedrito" <pixbypedrito at yahoo.comwrote:
Actually, the class doesn't necessarily have to be static, though static
does fit into the picture at some point, I suppose.

By default, C# (and I use VS.NET 2003, so I'm still in the dark ages),
creates a windows form app with the app main() startup in the same file as
the initial form.

What I normally do is break this out into a separate class called MainApp or
something like this. I then create an instance of my MainApp and have a
public static MainApp.App field to get to the instance from any class.

Then I can store all sorts of global goodies in my MainApp and access it
from any class. I find this to be very handy for doing things having an
application preferences which can be loaded on startup and saved on
shutdown, and what have you.
VS 2005 creates a Program.cs file by default that contains the entry
point and the call to Application.Run...very similar to what you're
doing manually right now.

Aug 31 '07 #11
On Aug 30, 9:36 pm, "pedrito" <pixbypedrito at yahoo.comwrote:
Actually, the class doesn't necessarily have to be static, though static
does fit into the picture at some point, I suppose.

By default, C# (and I use VS.NET 2003, so I'm still in the dark ages),
creates a windows form app with the app main() startup in the same file as
the initial form.
Fortunately this has changed in VS2005 - it creates Program.cs
separately from the main form. (It still creates Form1 by default,
which then doesn't get everything right when you rename it IIRC, so I
always delete Form1 and start again which is annoying... but never
mind!)

Jon

Aug 31 '07 #12
Jon Skeet [C# MVP] wrote:
Fortunately this has changed in VS2005 - it creates Program.cs
separately from the main form. (It still creates Form1 by default,
which then doesn't get everything right when you rename it IIRC, so I
always delete Form1 and start again which is annoying... but never
mind!)
Can you recall the specific example of having problems renaming Form1?
I don't like "Form1" as a name (too generic), and always rename my
default for "real" applications (ie, not just thrown together for
testing). I don't recall having any trouble with this. I just change
the name in the class declaration, and VS updates it everywhere else for
me when I choose the appropriate refactor command. If I want to change
the actual source file name, it is similarly simple; just change the
name in the Solution Explorer, and it's done.

In fact, it's one of the things with which I've been impressed by
VS2005; it handles a wide variety of renaming scenarios in a practically
transparent manner, no matter how many different places need to be
touched to make things consistent.

I'd be curious what pitfalls await me, so that I'm not too surprised
when I finally run into them. :)

Pete
Aug 31 '07 #13
Peter Duniho <Np*********@NnOwSlPiAnMk.comwrote:
Jon Skeet [C# MVP] wrote:
Fortunately this has changed in VS2005 - it creates Program.cs
separately from the main form. (It still creates Form1 by default,
which then doesn't get everything right when you rename it IIRC, so I
always delete Form1 and start again which is annoying... but never
mind!)

Can you recall the specific example of having problems renaming Form1?
I don't like "Form1" as a name (too generic), and always rename my
default for "real" applications (ie, not just thrown together for
testing). I don't recall having any trouble with this. I just change
the name in the class declaration, and VS updates it everywhere else for
me when I choose the appropriate refactor command. If I want to change
the actual source file name, it is similarly simple; just change the
name in the Solution Explorer, and it's done.
I'll try it now.

<sfx: tappety tapety>
In fact, it's one of the things with which I've been impressed by
VS2005; it handles a wide variety of renaming scenarios in a practically
transparent manner, no matter how many different places need to be
touched to make things consistent.

I'd be curious what pitfalls await me, so that I'm not too surprised
when I finally run into them. :)
:)

Well, the first is that Form1.cs really *isn't* a nice source file
name. Surely it's quite rare that anyone *would* want a form really
called "Form1". If only VS asked you for the name of the main form, or
gave you an option to cancel... even "MainForm" would be better than
"Form1". (I know, I could change the templates - but it's a shame that
I have to.)

Given that I'd always want to rename the source file, in my test I did
exactly that to start with - and it gave me the option of renaming the
class as well. I accepted. Sure enough, the name was changed. The name
of the form (as on the designer) wasn't though.

Is this a huge blow? No... but it *is* about as much effort as deleting
the form and starting again, and more effort than if the project
started without the form at all.

If you refactor using the refactor menu, it doesn't (IIRC) offer to
change the name of the source file, so that has to be done separately.

As I say, asking the developer up-front would have been a much nicer
solution, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 31 '07 #14
Jon Skeet [C# MVP] wrote:
Well, the first is that Form1.cs really *isn't* a nice source file
name. Surely it's quite rare that anyone *would* want a form really
called "Form1". If only VS asked you for the name of the main form, or
gave you an option to cancel... even "MainForm" would be better than
"Form1". (I know, I could change the templates - but it's a shame that
I have to.)
No disagreement there. I think the initial default form should have the
same name as the project at a minimum, if VS isn't going to ask you for
a specific name.

But that's not really what I was asking about.
Given that I'd always want to rename the source file, in my test I did
exactly that to start with - and it gave me the option of renaming the
class as well. I accepted. Sure enough, the name was changed. The name
of the form (as on the designer) wasn't though.
You mean the Text property of the form instance?

Okay, I can see that as being an example of "doesn't get everything
right" in the sense that a brand new form would inherit the actual name.
On the other hand, I can't think of a single application I've written
in .NET where the Text property of the form was left being identical to
the in-code name of the form (ie, the default value assigned to it).

Or do you mean something else?
Is this a huge blow? No... but it *is* about as much effort as deleting
the form and starting again, and more effort than if the project
started without the form at all.
If you delete the only form in the project and then create a new one,
does the Main() method get updated properly so that the
Application.Run() statement instantiates your new form class instead of
the original one?

I have to admit, it never occurred to me to do this rather than just
renaming the form, and I agree that deleting and creating a new one is
slightly easier than renaming both the class and the .cs file
individually. But as you noted above, if you rename the file first, the
class can be renamed automatically. And if you still need to go back
and fix up the Main() method, I'm not sure that deleting the form and
starting a new one is easier.
If you refactor using the refactor menu, it doesn't (IIRC) offer to
change the name of the source file, so that has to be done separately.
Is this also the "doesn't get everything right" you were talking about?

If I understand it correctly, to sum up:

-- rename the .cs file, and the form instance's Text property isn't
updated

-- rename the class first, and you still need to rename the .cs
file, and the form instance's Text property isn't updated
As I say, asking the developer up-front would have been a much nicer
solution, IMO.
I agree. Just wanted to make sure I wasn't overlooking some subtle "hey
this didn't get changed" bug in code where I've renamed my form classes
and .cs files. :)

Thanks,
Pete
Aug 31 '07 #15

"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
Jon Skeet [C# MVP] wrote:
[snip]
>Given that I'd always want to rename the source file, in my test I did
exactly that to start with - and it gave me the option of renaming the
class as well. I accepted. Sure enough, the name was changed. The name of
the form (as on the designer) wasn't though.

You mean the Text property of the form instance?
I suspect either that or the Name property (or both) is what he means.
Okay, I can see that as being an example of "doesn't get everything right"
in the sense that a brand new form would inherit the actual name. On the
other hand, I can't think of a single application I've written in .NET
where the Text property of the form was left being identical to the
in-code name of the form (ie, the default value assigned to it).

Or do you mean something else?
>Is this a huge blow? No... but it *is* about as much effort as deleting
the form and starting again, and more effort than if the project started
without the form at all.

If you delete the only form in the project and then create a new one, does
the Main() method get updated properly so that the Application.Run()
statement instantiates your new form class instead of the original one?
[snip]

No doubt it gets updated properly and that's a problem as well.

I do almost exactly what Jon does, but I'm doing it from 2003. My method of
doing it, in the actual mechanics of things, is to rename Form1.cs to
MainApp.cs and delete all the form code leaving only the main() and renaming
the class to MainApp.

Then I create my "first form" and whatever I'm calling it is what I put in
the Application.Run().

Personally, I think if VS.NET isn't going to ask you what to name the first
form, you'd be better off without it for this exact reason. I suppose you
could create a custom wizard to do it. Frankly, if someone would, it would
probably be worth a little money. All you'd have to do is give the option to
set the name of the first form and do everything else the same way VS.NET
does it. It'd definitely save me a few minutes for nearly every app I write
which adds up.

I don't know what new refactoring facilities VS.NET has, but I do know there
are some pretty good plugins for VS.NET that will do pretty comprehensive
refactoring.

I use visual assist which I find to be money well spent, especially coming
from the VS.NET 2003 point of view, as it has a lot of the facilities that
2005 finally got.


Aug 31 '07 #16
you can use application domain, like;
System.AppDomain.CurrentDomain.SetData("strVar_1", "The Value One")

in your first form or entry point, and if you need the strVar_1 anywhere in
your forms or classes you can call like ;

AppDomain.CurrentDomain.GetData("strVar_1")

.....

Adam
Sep 1 '07 #17
Jon Skeet [C# MVP] wrote:
On Aug 30, 9:36 pm, "pedrito" <pixbypedrito at yahoo.comwrote:
>Actually, the class doesn't necessarily have to be static, though static
does fit into the picture at some point, I suppose.

By default, C# (and I use VS.NET 2003, so I'm still in the dark ages),
creates a windows form app with the app main() startup in the same file as
the initial form.

Fortunately this has changed in VS2005 - it creates Program.cs
separately from the main form. (It still creates Form1 by default,
which then doesn't get everything right when you rename it IIRC, so I
always delete Form1 and start again which is annoying... but never
mind!)
This is only aesthetic, though. No end user is going to see the name of
the form, are they?

Cheers,

Cliff

--

Have you ever noticed that if something is advertised as 'amusing' or
'hilarious', it usually isn't?
Sep 3 '07 #18
Enkidu <en********@com.cliffp.comwrote:
Fortunately this has changed in VS2005 - it creates Program.cs
separately from the main form. (It still creates Form1 by default,
which then doesn't get everything right when you rename it IIRC, so I
always delete Form1 and start again which is annoying... but never
mind!)

This is only aesthetic, though. No end user is going to see the name of
the form, are they?
They're not going to see the names of my variables, either - but that
doesn't mean I call everything int1, int2, int3, int4 etc.

Names are an important part of code readability - and "Form1" tells me
*nothing* about what the form is for.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 3 '07 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
12
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of...
2
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.