日期:2014-05-19  浏览次数:20749 次

参数传递问题,
class   A   //窗体
{
string   str;
//str   的值通过窗口输入;
}

在类B中我需要A中输入的str值,
我应该怎么做?
class   B
{

A.Show();
//怎么将A中的str传到B中来?
}

谢谢各位!

------解决方案--------------------
假设你的输入是通过TextBox的话,A类中设置个属性,返回TextBox1.Text,在B类中访问A类的此属性
------解决方案--------------------
要获得str的话就
public string _Str
{
get{return str;}
set{str=value;}
}

------解决方案--------------------
try:

class A //窗体
{
private B m_b;
public A(B b)
{
m_b = b; // 把b类给传过来
}

string str;
// 你可以在A窗体的任何地方这样把str赋值给b的m_str
m_b.m_str = str;
}

class B
{
public string m_str;

A a = new A(this);
A.Show();

}

------解决方案--------------------
构造函数:

B
{
string ss
public B(string str)
{
ss=str;
}
}


A:

B frm=new B(this.text1.text);
frm.show();

------解决方案--------------------
1 : 将 A 中的参数 申明为
public static string str= " ";
在 A 中赋值之后,
在B 中 A a=new A(); a.str= 就可以了

2. 通过参数传递.
将 B 的构造函数 该一下 入 Class B{
public string _c;
public B(string c)
{
_c=c;
}
}
在A 中 B b=new B( "传值 "); 在B 中 直接得到 c 就可以了

3.通过属性

namespace ReadFile
{
public partial class Form2 : Form
{


public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

Form3 fm = new Form3();
fm._val = this.textBox1.Text;
if (fm.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = fm._val;
}


}
}
}


namespace ReadFile
{
public partial class Form3 : Form
{
// 定义一个属性
public string _val;
public string val
{
get
{
return _val;
}
set
{
_val = value;
}
}

public Form3()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
_val = this.textBox1.Text;
this.DialogResult = DialogResult.OK;
this.Hide();
}

private void Form3_Load(object sender, EventArgs e)
{
this.textBox1.Text = _val;
}


}
}

4..委托 .. 太多了
------解决方案--------------------
直接将A类的TextBox设为Public,然后在B中直接掉,就OK了;

B{
A.ShowDialog();
A.close();
string temp = A.TextBox.text;
}