473,499 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating a global variable how to ?

Ok,
I am a VB developer but need to do a C# program, my question is how do I
decalre a global variable in c#, in VB.net I juest create a procedure an
declare my Variable as Public myVar and I can use it in all the program but
how can I do that inc C# ?

Thanks
Nov 17 '05 #1
7 2040
public sealed class ApplicationVariables
{
private ApplicationVariables()
{
}

private static string stringVariable;
public static string StringVariable
{
get{return stringVariable;}
set{stringVariable = value;}
}
}

if its readonly you gain a bit performance by marking readonly. for example
:

public sealed class ApplicationVariables
{
private ApplicationVariables()
{
}

private static readonly string stringVariable =
System.Configuration.ConfigurationSettingsAppSetti ngs["StringVariable"];
public static string StringVariable
{
get{return stringVariable;}
}
}

compiler will optimise this.
Nov 17 '05 #2
Just create a public variable or property (of the desired type) inside a
public class and start using it by qualifying with namspace name (if in a
different namespace) and class name.

"Carlos" <cp@swa.com> wrote in message
news:O5**************@TK2MSFTNGP10.phx.gbl...
Ok,
I am a VB developer but need to do a C# program, my question is how do I
decalre a global variable in c#, in VB.net I juest create a procedure an
declare my Variable as Public myVar and I can use it in all the program but
how can I do that inc C# ?

Thanks

Nov 17 '05 #3
Ok I do the following
MySpace.MyClass. but I do not see me public variable defined in my public
class ?
"Siva M" <sh******@online.excite.com> wrote in message
news:uV**************@TK2MSFTNGP14.phx.gbl...
Just create a public variable or property (of the desired type) inside a
public class and start using it by qualifying with namspace name (if in a
different namespace) and class name.

"Carlos" <cp@swa.com> wrote in message
news:O5**************@TK2MSFTNGP10.phx.gbl...
Ok,
I am a VB developer but need to do a C# program, my question is how do I
decalre a global variable in c#, in VB.net I juest create a procedure an
declare my Variable as Public myVar and I can use it in all the program
but
how can I do that inc C# ?

Thanks

Nov 17 '05 #4
Did you declare you variable as public? Here is an example (both in the same
namespace):

public class MyClass
{
public int MyIntVar = 0;
public static string MyMsg = "Hello";
}

public class AnotherClass
{
public void AnotherClassMethod()
{
MyClass m = new MyClass();
Console.WriteLine (m.MyIntVar);
Console.WriteLine (MyClass.MyMsg);
}
}

If the variables are public and static then they can be accessed without
instantiating the containing class (MyMsg in the above example).

"Carlos" <cp@swa.com> wrote in message
news:OH**************@TK2MSFTNGP09.phx.gbl...
Ok I do the following
MySpace.MyClass. but I do not see me public variable defined in my public
class ?
"Siva M" <sh******@online.excite.com> wrote in message
news:uV**************@TK2MSFTNGP14.phx.gbl...
Just create a public variable or property (of the desired type) inside a
public class and start using it by qualifying with namspace name (if in a
different namespace) and class name.

"Carlos" <cp@swa.com> wrote in message
news:O5**************@TK2MSFTNGP10.phx.gbl...
Ok,
I am a VB developer but need to do a C# program, my question is how do I
decalre a global variable in c#, in VB.net I juest create a procedure an
declare my Variable as Public myVar and I can use it in all the program
but
how can I do that inc C# ?

Thanks


Nov 17 '05 #5
Yes, but when you do a new do not you loose the previous value ?

"Siva M" <sh******@online.excite.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Did you declare you variable as public? Here is an example (both in the
same
namespace):

public class MyClass
{
public int MyIntVar = 0;
public static string MyMsg = "Hello";
}

public class AnotherClass
{
public void AnotherClassMethod()
{
MyClass m = new MyClass();
Console.WriteLine (m.MyIntVar);
Console.WriteLine (MyClass.MyMsg);
}
}

If the variables are public and static then they can be accessed without
instantiating the containing class (MyMsg in the above example).

"Carlos" <cp@swa.com> wrote in message
news:OH**************@TK2MSFTNGP09.phx.gbl...
Ok I do the following
MySpace.MyClass. but I do not see me public variable defined in my
public
class ?
"Siva M" <sh******@online.excite.com> wrote in message
news:uV**************@TK2MSFTNGP14.phx.gbl...
Just create a public variable or property (of the desired type) inside a
public class and start using it by qualifying with namspace name (if in a
different namespace) and class name.

"Carlos" <cp@swa.com> wrote in message
news:O5**************@TK2MSFTNGP10.phx.gbl...
Ok,
I am a VB developer but need to do a C# program, my question is how do I
decalre a global variable in c#, in VB.net I juest create a procedure an
declare my Variable as Public myVar and I can use it in all the program
but
how can I do that inc C# ?

Thanks


Nov 17 '05 #6
Yes, each instance get a new value. If you need to retain values, then go
for static variables, which can be accessed without instantiation.

"Carlos" <cp@swa.com> wrote in message
news:eR**************@TK2MSFTNGP09.phx.gbl...
Yes, but when you do a new do not you loose the previous value ?

"Siva M" <sh******@online.excite.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Did you declare you variable as public? Here is an example (both in the
same
namespace):

public class MyClass
{
public int MyIntVar = 0;
public static string MyMsg = "Hello";
}

public class AnotherClass
{
public void AnotherClassMethod()
{
MyClass m = new MyClass();
Console.WriteLine (m.MyIntVar);
Console.WriteLine (MyClass.MyMsg);
}
}

If the variables are public and static then they can be accessed without
instantiating the containing class (MyMsg in the above example).

"Carlos" <cp@swa.com> wrote in message
news:OH**************@TK2MSFTNGP09.phx.gbl...
Ok I do the following
MySpace.MyClass. but I do not see me public variable defined in my
public
class ?
"Siva M" <sh******@online.excite.com> wrote in message
news:uV**************@TK2MSFTNGP14.phx.gbl...
Just create a public variable or property (of the desired type) inside a
public class and start using it by qualifying with namspace name (if in a
different namespace) and class name.

"Carlos" <cp@swa.com> wrote in message
news:O5**************@TK2MSFTNGP10.phx.gbl...
Ok,
I am a VB developer but need to do a C# program, my question is how do I
decalre a global variable in c#, in VB.net I juest create a procedure an
declare my Variable as Public myVar and I can use it in all the program
but
how can I do that inc C# ?

Thanks



Nov 17 '05 #7
siva, marking class sealed prevents inheriting from that class and causes
some compiler optimizations. providing private constructor prevents this
class to be instantinated as all methods - members will be static. expanding
fields to outside of the class making the field public is not a good
practice. encapsulating with properties is better. if properties just
returns-sets specific fields, compiler is clever enough to give access to
field directly behind the scenes..
Nov 17 '05 #8

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

Similar topics

14
8839
by: RooLoo | last post by:
Hey all In my GLOBAL.ASA file I'm trying to create a session variable for reference in the various webpages of my site.... Sub Session_OnStart Session("LoggedOn")="Y" End Sub When...
22
3096
by: Tom Moroow | last post by:
Hi, I'm pretty new to javascript and was wondering how you would piece together a variable name and then assign it a value. I want to create a hidden field and assign it a value based on the value...
8
3983
by: mcmg | last post by:
Hi, I have an asp app that works fine on a windows xp machine but does not work on a windows 2000 server. I have the following code in my global.asa: <OBJECT RUNAT=Server SCOPE=SESSION...
10
2068
by: connyledin | last post by:
Im trying to create a version of the game Wumpus. Mine is called Belzebub. But im STUCK! And its due tuesday 2 maj. Im panicing! Can some one help me?? here is the file:...
1
2499
by: MAB | last post by:
I need to create a service (or something else) to monitor the coming and goings of files (& possible folders) beneath a root folder. This service need to know the path/FileName of any new files so...
0
1120
by: Tarun Mistry | last post by:
Hi all, I'm trying to create a global object that can be used anywhere in my ASP.NET (2.0) application but im having problems. I would like to run some logic every time the page is loaded,...
9
2982
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
1
29300
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
5344
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
7009
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...
1
6899
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
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4919
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...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.