The class that contains the Main() method is always a bit confusing.
There is a way of using the Main method that may help you along here.
First, some background information. The "world" inside your class
definition is divided into two separate parts: "instance things" and
"static things". Instance things (fields, methods, etc) exist
separately for each object that you create from the class. So, if you
declare an instance member:
class Class1 {
int count = 0;
...
then every time you create a "new Class1()", you will get a new "count"
along with it. You can work with "count": read it, manipulate it... but
only from within _instance methods_. That is, only from methods and
properties that belong to this "instance world" within your class. It's
a good habit to get into to always refer to your instance fields by
preceding the reference with "this.", like this:
this.count += 1;
The other world is "static stuff". Static fields, methods, and
properties exist once for the class as a whole. So, for example, if you
were to declare:
public class Class1 {
static int static_count = 0;
...
there is only one static_count shared amongst all instances of your
Class1. Every Class1 object can see the same static_count. You refer to
static fields, properties, etc. by preceding them by the class name:
Class1.static_count += 1;
Now, if you don't indicate to the compiler whether you want an instance
member or a static member (that is, if you mention the name of
something without specifying "this." or "Class1.", then the compiler
will simply find the item by its name, and deal with it accordingly.
This is very easy for the compiler, but difficult for human readers, so
I prefer to use "this." and "Class1." all over the place to make it
clear.
Now, here's the rub: the individual objects that you instantiate from
your class can all see static members: all they have to say is
Class1.static_count and they find the field. _However_, (and this is
the important part) static methods and properties _can't_ see instance
members: if you're in your "static void Main" method, and you say
"this.count", which instance are you talking about? There's only one
Main method, shared amongst all instances. If you make 500 instance of
your object, and in your Main method you say "this.count", which one of
the 500 counts out there are you referring to? Of course, if you have
an object reference, you can certainly do it:
public static void Main()
{
Class1 c1 = new Class1();
c1.count += 1;
}
but here you've specifically said "c1's count". You can't say
"this.count" because in a static method, there is no instance... no
"this".
So, here is the advice about structuring Main methods: don't do
anything in your Main method. You can just instantiate the object that
contains your program, perhaps call some sort of Run() method or
something, and that's it. Here's an example:
public Class1
{
public Class1()
{
... set up your class...
}
public void Run()
{
... do whatever your program needs to do...
}
// Very simple Main method that just creates an instance of the
class and kicks off the program.
public static void Main()
{
Class1 myProgram = new Class1();
myProgram.Run();
}
}
This gets you out of the "static" world and into the instance world as
soon as possible. Certainly it's debatable practice: many people prefer
to write most of their logic in Main(), but if you're going to have
both a static Main and an instance, I'd rather keep everything on one
side of the fence, so to speak, rather than have one foot in each camp
and try to keep it all straight.
Particularly while you're learning the language and the concepts, you
might start off this way to keep things simpler.