Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
Hello,

It has been a long time since I wrote a program but I decided that I want to learn C#...

I am writting a Windows Form application that has some fields. These fields are used to define the size of an multi-dimensional array. Once the fields are input by the form user and the set value button is clicked, the rest of the forms buttons become available.

Question, how do I pass the array from method to method, if I am creating the array inside of the setvalues_click method?

C#
namespace WFRobotCar
 {
 private void Setvalue_Click(object sender, EventArgs e)
    {
       Myrc array_rc = new Myrc(Convert.ToInt32(this.XtextBox.Text), Convert.ToInt32(this.YtextBox.Text));
    }

 private void printrc_click(object sender, EventArgs e)
    {
      int i, j;
      for (int i = 0; i < array_rc.GetLength(0); i++)
        {
            Console.Write("     ");
            for (int j = 0; j < array_rc.GetLength(1); j++)
            {
                if ((i == int_current_row) && (j == int_current_col))
                    Console.ForegroundColor = ConsoleColor.Green;
                if (array_rc[i, j])
                {
                    Console.Write('-');
                }
                else
                {
                    Console.Write('0');
                }
                if ((i == int_current_row) && (j == int_current_col))
                    Console.ResetColor();
            }
            Console.WriteLine();
        }

    }
 }



Thanks in advanced,

Gladys
Posted
Updated 10-Mar-11 3:59am
v2

Just make the array global to the form and get on with it. In fact, I would use a List instead of an array because a List can grow/shrink on demand.
 
Share this answer
 
This can be a solution :

C#
namespace WFRobotCar
 {
     Myrc array_rc;

 private void Setvalue_Click(object sender, EventArgs e)
    {
       array_rc = new Myrc(Convert.ToInt32(this.XtextBox.Text), Convert.ToInt32(this.YtextBox.Text));
    }
 private void printrc_click(object sender, EventArgs e)
    {
      int i, j;
      for (int i = 0; i < array_rc.GetLength(0); i++)
        {
            Console.Write("     ");
            for (int j = 0; j < array_rc.GetLength(1); j++)
            {
                if ((i == int_current_row) && (j == int_current_col))
                    Console.ForegroundColor = ConsoleColor.Green;
                if (array_rc[i, j])
                {
                    Console.Write('-');
                }
                else
                {
                    Console.Write('0');
                }
                if ((i == int_current_row) && (j == int_current_col))
                    Console.ResetColor();
            }
            Console.WriteLine();
        }
    }
 }
 
Share this answer
 
private void Setvalue_Click(object sender, EventArgs e)
{
   Myrc array_rc = new Myrc(Convert.ToInt32(this.XtextBox.Text), Convert.ToInt32(this.YtextBox.Text));

   MyMethod(array_rc)
}


void MyMethod(MyRC arrayIn)
{
// use the array here
}


Maybe buy a C# book to learn this basic stuff?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900