1) why cant my static function see the class variables and a non-static method can:

example:

class Chess
    {
        int columnStart;
        int rowStart;
        int columnEnd;
        int rowEnd;

        public static void Main(string[] args)
        {
          int[] locationMap = new locationMap[4];

                 columnStart = locationMap[0];
                 rowStart = locationMap[1];
                 columnEnd = locationMap[2];
                 rowEnd = locationMap[3];          
 
        }

i am trying to do the following manipulation. But i am not sure that it works?!?

public static void Main(string[] args)
        {
            int columnStart;
            int rowStart;
            int columnEnd;
            int rowEnd;
            int b = 0;
            Pieces[,] pieces = ChessBoard();

            do
            {
                // prints chessboard
                printChessBoard(pieces);

                //Users Answer
              i want all the location map array variables to get values once the     return from the function (the method. assigns them values at the end of the operation).
                User c = new User(pieces);
                int[] locationMap = c.answerReturned(pieces);


                Game g = new Game(pieces, locationMap[0],locationMap[1],locationMap[2], locationMap[3]);
                pieces = g.makeMove();


                //Once those location variables return , i want the new variables to get them.
                rowStart = locationMap[0];
                columnStart = locationMap[1];
                rowEnd = locationMap[2];
                columnEnd  = locationMap[3];


               

                //Promotion
// i want the new variables assigned above to have the same values as those parameters
                Promotion(pieces, rowStart, columnStart, rowEnd, columnEnd);

                b++;
            } while (b < 50);

thank you!! rep will be given with any help!!

1. All variables declared in class are by default private. If you want that they can be visible to any method, try putting keyword public before data type.

public int variable1;

2. If you have location map, I think that there must be X and Y cooridnate?

no.. i just named it location..


i tried to declare it as public , still shows an error

1. A static method belongs to the class, not to an instance of the class, so the instance variables have no meaning in a static context. You need to create an instance of your class in the static method, then access any public instance variables, properties or methods of the instance.

2. I don't really understand what you are trying to do sorry. Without access to your answerReturned method or the Promotion method, I can't really see what you are doing.

commented: Thank you +0

This is my main:

public static void Main(string[] args)
        {
            int columnStart;
            int rowStart;
            int columnEnd;
            int rowEnd;
            int b = 0;
            Pieces[,] pieces = ChessBoard();

            do
            {
                // prints chessboard
                printChessBoard(pieces);

                //Users Answer
                User c = new User(pieces);
                int[] locationMap = c.answerReturned(pieces);

                //Games new board to be printed
                Game g = new Game(pieces, locationMap[0], locationMap[1], locationMap[2], locationMap[3]);
                pieces = g.makeMove();

                //PieceLocation 
                rowStart = locationMap[0];
                columnStart = locationMap[1];
                rowEnd = locationMap[2];
                columnEnd  = locationMap[3];

                //Castling
                Castling castle = new Castling();
                castle.CastlingCheck(pieces, rowStart, columnStart, rowEnd, columnEnd);

                //Promotion
                Promotion(pieces, rowStart, columnStart, rowEnd, columnEnd);

                b++;

            } while (b < 50);
        }

it doesnt matter what each method does. what i really dont understand is when i use method, variables are transferred to another method or variable in the main after the particular method was called. Why it happens , is what confuses me and therefore making me more prone to mistakes!!

Here is my promotion static method:

public static Pieces[,] Promotion(Pieces[,] pieces, int rowStart, int columnStart, int rowEnd, int columnEnd)
        {
            PiecePromotion promotion = new PiecePromotion();
            Object[,] choice2 = new Object[1, 5];
            Object[,] choice = new Object[1, 5];
            bool whitePromo = false;
            bool blackPromo = false;

            if (pieces[rowEnd, columnEnd] != null && pieces[rowEnd, columnEnd].pieceName()[1] == 'P' && (rowEnd == 1 || rowEnd == 8))
            {

                if (pieces[rowEnd, columnEnd].pieceName()[0] == 'B')
                {

                    blackPromo = true;
                }
                else
                {
                    whitePromo = true;
                }

                promotion.promotionPresentation(choice, choice2, blackPromo, whitePromo);

                if (whitePromo)
                {
// the promote method simply modifies my Pieces[,] array and returns it modified.
// i put "pieces= " so that pieces will obtain a new value.
// but what if i didnt put "pieces=", would the "return" declaration return me the modified array or not?
                    pieces = promotion.promoteWhite(pieces, rowStart, columnStart, columnEnd, rowEnd, choice2);
                }
                else
                {
                    pieces = promotion.promoteBlack(pieces, rowStart, columnStart, columnEnd, rowEnd, choice);
                }
            }
            return pieces;
        }

As you can see towards the end i call another method and make it equal to pieces.
even if i remove "pieces=" it still compiles. But would it return me the new pieces after it was modified by the method i called?

To answer your first question: no, the "pieces" array back in your main method would not be modified unless you set the "pieces" array in your Promotion method to a new value and returned it. This is because C# by default does something called "pass-by-value". It means that when you pass a variable to a method, the method creates a copy of that variable and modifies the copy, not the original.

The second problem I'm noticing in your code is that you don't seem to understand what a "static" method is. Saying a method is "Static" means that you can use the method without actually creating an instance of the class. For example, this code is illegal:

class sampleClass1
   {
   public void method1()
      {
      }
   }

class sampleClass2
   {
   public static void Main()
      {
      //This will throw an error
      sampleClass1.method1();
      }
   }

You can't call the method because you haven't created a class to call it from. However this code is perfectly fine:

class sampleClass1
   {
   //Now the method is static
   public static void method1()
      {
      }
   }

class sampleClass2
   {
   public static void Main()
      {
      //This will run now
      sampleClass1.method1();
      }
   }

Now that the method is static we can call it without having to create a class to call it from.

The reason "Main" is always static is because, at the beginning of our program, there are no classes to call methods from. Hence we need a method we can run without having to create a class. The problem with static methods is, as you noticed, that they can't access global variables or any values that change in a class (thus making them pretty useless for most OO tasks).

Unlike in C, where lots of work is usually done in the Main class, in C# the Main method typically just creates a few classes, calls a method to get the program going, and then stops. A better form for your code would be something like this:

class Chess
   {
   /* all of your global variables */

   public static void Main()
      {
      Chess chessgame = new Chess();
      chessgame.beginGame();
      }

   public void beginGame()
      {
      /*All the code that is currently in your Main method*/
      }
   }
commented: Thank you for the explanation +0

Thats what i did. i only activate a loop in the main which runs. My classes and static methods handle the rest. what i do. The main calls method to:
print the board, obtain user input, check if castling is possible, checks if there there is a check/mate or piece promotion.


The hole in my understanding concentrate on passed-by-value.


As to the last part of the main code:

//PieceLocation 
                rowStart = locationMap[0];
                columnStart = locationMap[1];
                rowEnd = locationMap[2];
                columnEnd  = locationMap[3];
 
                //Castling
                Castling castle = new Castling();
                castle.CastlingCheck(pieces, rowStart, columnStart, rowEnd, columnEnd); // will y rowStart, columnStart,,parameters obtain the variables that i fed in from above?
 
                //Promotion
                Promotion(pieces, rowStart, columnStart, rowEnd, columnEnd);

That kind of idea i dont understand too. how did numbers get updated if it was updated?

i didnt do

numbers=modifyArray(numbers);

amespace ConsoleApplication1
{
    class Program
    {

public static void Main(string [] arg)
{
     int[] numbers={32,32,12,32,32,1,2,3,4,};
     
     modifyArray(numbers);

     foreach(int item in numbers)
     {
         Console.WriteLine(item); // this will print all the new array, even though 
//the modifyArray method didnt even return a value. why i can modify arrays or //variables.. even when i use methods that dont return anything?
      }


}
  public static void modifyArray(params int[] nums)
 {
   int [] newArray=new int[10];
   for(int i=0;i<nums.Length;i++)
   {

       nums[i] = nums[i] * 2;
   }
   }
  }
}

I have to apologize: the answer I gave you before was wrong. The array will be modified by the method.

When you make an array in C# the array variable doesn't hold the values of the array. Instead, it holds the location in the computer's memory where the array values are located.

e.g.

int b = 2;
int[3] a = {1, 2, 3};

"b" contains the value 2, but "a" contains an address in the computer's memory where the values for 1, 2, and 3 are stored.

When you pass an array to a function it copies the address to a new local variable. Thus when you modify the local variable, it modifies the memory where the array is stored. When you use the variable in the function that called it the values have changed because the memory the address points to has changed.

public void method1()
   {
   //array1 points to a place in memory where 1, 2, and 3 are stored
   int[3] array1 = {1,2,3};

   //array1 is passed into method2
   method2(array1);

   //array1 goes to the place in memory its address points to and prints the
   //first number it finds there. This is the same address array2 pointed to.
   //Since array2 modified that number, the number array1 pulls from memory will
   //have changed.
   Console.WriteLine(array1[0].ToString());
   }

//The address stored in array1 is copied into array2
public void method2(int array2[])
   {
   //array2 goes to the memory location its address points to and changes
   //the value for the first number there 
   array2[0] = 3;
   }

What confused me before was that all the sources I looked at said that C# passes-by-value. However some data types contain values (and need a "return" action to change their values) and some contain addresses (and can be changed by the method).

The general rule is that the basic data types (such as int, char, etc.) pass their values to functions, while larger, more complex data types (such as arrays or classes) pass their addresses. Sorry for the confusion this must have caused.

so the more complex data pass the info by ref. is that right.?


and i can modify that type of data with methods without returning them?

no matter if the method call is before or after the array?

To be really technical we say that the more complex types (mostly those that inherit from the object class) are "reference types". They're still passed by value into methods, it's just that the value they pass is an address.

But yes, when these reference types are passed into a function you can modify them and change their values without having to use a return statement.

I'm not sure what you mean by "no matter if the method call is before or after the array". Could you please describe what you mean here?

it doesnt matter. i think it is clear to me now. thanx for the help. you filled a hole in my understanding.. i was to afraid to ask this question in class, cause i thought it would be stupid..

No problem, glad to help (I clarified my own C# understanding as well).

I know it's cliched to say: but don't be afraid to ask questions in class. I can't tell you how many times in university the only reason I understood something was because a classmate asked the teacher about it. Especially when it comes to memory pointers. Nobody gets that stuff on their first try :D.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.