By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,240 Members | 791 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,240 IT Pros & Developers. It's quick & easy.

Passing Arrays Quest.

P: n/a
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];
}
}
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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"
}
}
--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2

P: n/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
Nov 16 '05 #3

P: n/a
Ignore thread- problem solved - stupid mistake called wrong method- too many
methods with similar names thank you

"Steve B." wrote:
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

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.