473,795 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global variables & arrays

Hi,

I am new to C#. I am trying to create some variables and arrays that can be
seen throughtout the whole program. I have no choice as they have to be seen
by the whole program. Where and how do I declare them? I tried putting the
variable gnSum and gaArray in my Main() method but the rest of the program
does not see them.
class RefValTest

{

public static int Main()

{

int gnSum=0; // global variable

int[] gaArray= new int [7]; // global array

return 0;

}

}

TIA
Roy
Nov 17 '05 #1
1 17603
Hi Roy,

First of all ...

It is never the case, that an array or a variable has to be seen by the
whole program. sometimes this
is the case for constants, but not for variables. You're working in an
object-oriented environment,
and you should change the design of your program accordingly.

If you're completely new to object-oriented programming, you should make
yourself familiar with it,
since it has nothing to do with structured programming.

But anyway ... here's a way how you can make your variable global:

Create a new class and declare the variable as a static field:

public class GlobalValues {

public static int GlobalVariable = 0;

}

Now you can access it from anywhere using:

GlobalValues.Gl obalVariable = ...;

And btw.: No, there is no way to just declare a variable without putting it
inside a Datatype.

REgards,

Frank Eller
"Roy Gourgi" <ro***@videotro n.ca> schrieb im Newsbeitrag
news:hB******** ************@we ber.videotron.n et...
Hi,

I am new to C#. I am trying to create some variables and arrays that can
be seen throughtout the whole program. I have no choice as they have to be
seen by the whole program. Where and how do I declare them? I tried
putting the variable gnSum and gaArray in my Main() method but the rest of
the program does not see them.
class RefValTest

{

public static int Main()

{

int gnSum=0; // global variable

int[] gaArray= new int [7]; // global array

return 0;

}

}

TIA
Roy

Nov 17 '05 #2

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

Similar topics

2
3180
by: R. Rajesh Jeba Anbiah | last post by:
I'm supposed to do a big portal with multi-language support and I'm decided to do my own instead of going for gettext. I have sort out few implementations; most of them uses global variables. I'm bit confused, how it will affect the performance. Right now, I use global variables in config file. Most of the experts in this group are ranting against global variable usage. I would like to know how would you handle such config settings (DB is...
13
4866
by: Larry L | last post by:
I have a Module that declares several arrays as public, some string, some integers. I then have 2 forms. Form A is the main form, that loads on start-up, and has a command button to open Form B. On form B there are several text boxes where the array values can be changed, however when I close B and go back to A, those values are lost. I thought Public meant Public? Larry L
2
9171
by: Patient Guy | last post by:
I have a library of functions representing a filesystem interface (essentially a file selection interface, to be used in opening/reading/writing/closing files). Heavily scripted HTML document #1, very application-like, must include the JS file for this library of functions. One reason is that it must call a function to name a "callback" function, and within its own script block, must define the callback function. The callback handles...
33
3052
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 been for years. If the machine has memory to spare and windows can use it - I'm thinking "Why not?" I was wondering what some of you have to say about that, particularly any severe "gotchas" you've had the unfortunate experience to contend with.
3
22815
by: Rahul Gandhi | last post by:
Hi, Which one preferable with respect to code size of the executable Un-initialised global variables or initialised global variables regards Rahul
10
2645
by: Kleenex | last post by:
Reason: I am working on an embedded project which has very limited memory (under 512 bytes, 60 or so of which is stack space), which translates into limited stack space. In order to save on stack space, I tried to only use parameters and stack space for things which are truely temporary. Instead of passing a pointer to a data structure which should always be populated with data, I have the data structure declared as a global variable and...
4
1433
by: Sheldon | last post by:
Hi, I have a series of classes that are all within the same file. Each is called at different times by the main script. Now I have discovered that I need several variables returned to the main script. Simple, right? I thought so and simply returned the variables in a tuple: (a,b,c,d,e) = obj.method() Now I keep getting this error: "ValueError: unpack tuple of wrong size"
112
5486
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 that may print some messages. foo(...) { if (!silent)
18
2071
by: Bruce | last post by:
When I do this: <input id="someName" type="text"> <script type="text/javascript"> alert(someName.nodeName); </script> Both IE6 and Firefox alert("INPUT"). Why isn't "someName" undefined?
0
9522
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10448
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10167
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9046
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7544
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2922
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.