Passing Arrays Quest.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can't pass an array from one class to another.
Steve

Class Form1
{
public string[] dGridTitles = new string[5]
{"xyz",,,,,};

public string[] getdGridTitles()
{
string [] strgRay = new string[dGridTitles.Length];
dbaseTableNames.CopyTo(strgRay, 0);
return strgRay;
}
}

Class Form2
{
Form1 form1 = new Form1();

///getdGridTitles() returns INVALID array
int z = form1.getdGridTitles ().leng; //wrong length
method1(form1.getdGridTitles ()) ;

///gets ray-OK but can't pass on...?
form1.dGridTitles;
}

//never got this far
Class Implement ///dBtemplate/interface
(
method1(string[] Titles)
{
string x = title[1];
}
}
 
Hi Steve,

Your code seems to be full of little errors, none too major but they all
add up.
The code below works, but I'm not sure it does what you want.

public class Test : Form
{

public Test()
{
Form2 f = new Form2();
f.DoStuff();
}

[STAThread]
public static void Main()
{
Application.Run(new Test());
}
}

class Form1
{
// 'new string[5]' isn't really necessary when you are assigning values
public string[] dGridTitles = /*new string[5]*/{"xyz","a","b","c","d"};

public string[] getdGridTitles()
{
string [] strgRay = new string[dGridTitles.Length];
dGridTitles.CopyTo(strgRay, 0);
return strgRay;
}
}

class Form2
{
public void DoStuff()
{
Form1 form1 = new Form1();
Implement imp = new Implement();

imp.method1(form1.getdGridTitles()) ;
}
}

class Implement
{
public void method1(string[] titles)
{
string x = titles[1];
// x is now "a"
}
}
 
Morten,

Thank you for your post. I didn't want to bury you with details at first in
case I was not handling the array incorrectly so here are some more details.
Should I be passing the "this" pointer somewhere ? It seems passing the
array corupts it.


public class Test : Form
{
public menu_click()
{
Form1 log = Form1.createInstance();
log.ShowDialog();
}
}

class Form1: Implement, Interface
{
public string[] dGridTitles = {"a","b","c","d"};

[STAThread] ///do I need this ???
public Form1()
{
}

[STAThread] ///do I need this ???
public static Form1 createInstance()
{
Form1 form1 = new Form1();
return form1;
}

public string[] getdGridTitles()
{
string [] strgRay = new string[dGridTitles.Length];
dGridTitles.CopyTo(strgRay, 0);
return strgRay;
}
}

class Form2: Implement, Interface
{

Form2
{
Form1 form1 = Form1.createInstance()
}

fom2_load()
{
///method1 parameter: form1.getdGridTitles() -returns WRONG
///array values when used as in example below
method1(form1.getdGridTitles()) ;
}
}

class Implement: Form
{
public void method1(string[] titles)
{
string x = titles[1]; //gets wrong value
}
}

interface Interface
{
void method1(string[] titles);
}


Steve
 
Ignore thread- problem solved - stupid mistake called wrong method- too many
methods with similar names thank you
 
Back
Top