I'm currently learning C# and i'm trying to pass array values between two forms, but I keep getting errors. Here is the code i'm trying to use:
*******FORM1 (JetBulls)********
string[] name = new string[15];
string[] myArray2;
myArray2 = Seats.passValuesMethod(myNewArray);
*******FORM2 (Seats)********
public string[] passValuesMethod(string[] myNamesArray)
{
string[] myNewArray = new string[15];
return myNewArray;
}
***
If i place "myArray2 = Seats.passValuesMethod(myNewArray);" outside of a method, i get invalid token errors
If i place it inside of a method, i get System.Windows.Forms.Form does not contain a definition for 'passValuesMethod' and it says the value i'm try to pass doesn't exist in the current context.
Any ideas what i'm doing wrong? Thanks.
Loading
Kirtan PatelPosted Oct 31, 2009, 1:32 PM
you dont have to write that Class Code in Your Form Code
In Solution Explorer
Create New Class File and Write that Class1 Code in That File like Below Image
and Friend,
Dont Forget to mark "Do you like this answer" :)
drewPosted Nov 1, 2009, 11:17 AM
after some re-factoring, I got it working.
drewPosted Oct 31, 2009, 1:22 PM
I put the class1 code on form 1 (the main entry point form) should it be somewhere else? Here is where i placed it:
namespace JetBulls
{
public partial class JetBulls : Form
{
//temporary array class
class Class1
{
public static string[] tempArray = new string[5];
}
}
}
Kirtan PatelPosted Oct 31, 2009, 12:58 PM
you need to Create One Class File Which Will work as Mid Interface between Form 1 and Form 2
friend, check "do you like this answer" if my answer helps you :)
Class1 Code
--------------
namespace WindowsFormsApplication6
{
class Class1
{
public static string[] temparray = new string[5];
}
}
Form 1 Code
------------------
private void Form1_Load(object sender, EventArgs e)
{
//form1array is array which you wnt to transfer to form 2
string[] form1array =new string[5];
form1array[0] ="test";
form1array[1] ="test1";
form1array[2] ="test2";
form1array[3] ="test3";
form1array[4] ="test4";
Class1.temparray = form1array;
}
Form2 Code
---------------
private void Form2_Load(object sender, EventArgs e)
{
//retrive the array from class that is sent from Form1 to Class1
string[] form2array = Class1.temparray;
}
drewPosted Oct 31, 2009, 12:44 PM
Kirtan PatelPosted Oct 31, 2009, 11:50 AM
pass array in Form 1 to Form 2 ???