Global variables & arrays

R

Roy Gourgi

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
 
N

news.microsoft.com

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.GlobalVariable = ...;

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

REgards,

Frank Eller
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top