传参就可以了呀 public class aaa { public string[] hx() { 这里是一个接收串口数据,想在这里将接收到的数据传回到主程序里 return str; } } 主程序: aaa a = new aaa(); string[] str=a.hx();
可以的 public class intterface { public string out() { string a=.... return a; } } 调用 intterface a = new intterface(); string result=a.out();
可用 Delegate 实现,像这样: using System;// delegate declaration delegate void MyDelegate();public class MyClass { public void InstanceMethod() { Console.WriteLine("A message from the instance method."); } static public void StaticMethod() { Console.WriteLine("A message from the static method."); } }public class MainClass { static public void Main() { MyClass p = new MyClass(); // Map the delegate to the instance method: MyDelegate d = new MyDelegate(p.InstanceMethod); d(); // Map to the static method: d = new MyDelegate(MyClass.StaticMethod); d(); } }
to amendajing(被压迫,闭关学习!) ;要实时的哟?就是收到数据就传递信息?你那个不能实现功能!
{
public string hx()
{
string a=这里是一个接收串口数据,想在这里将接收到的数据传回到主程序里
return a;
}
}
主程序:
aaa a = new aaa();
string result=a.hx();
public class aaa
{
public void hx(string[] str)
{
这里是一个接收串口数据,想在这里将接收到的数据传回到主程序里
return str;
}
}
主程序: aaa a = new aaa();
string[] str=a.hx();
在AAA里定义一个delegate,在new这个aaa后把主程序的一个过程绑定到对应的委托上,
当在aaa里调用这个委托时不会触发主程序的那个方法。
public class aaa
{
public string[] hx()
{
这里是一个接收串口数据,想在这里将接收到的数据传回到主程序里
return str;
}
}
主程序: aaa a = new aaa();
string[] str=a.hx();
public class intterface
{
public string out()
{
string a=....
return a;
}
}
调用
intterface a = new intterface();
string result=a.out();
using System;// delegate declaration
delegate void MyDelegate();public class MyClass
{
public void InstanceMethod()
{
Console.WriteLine("A message from the instance method.");
} static public void StaticMethod()
{
Console.WriteLine("A message from the static method.");
}
}public class MainClass
{
static public void Main()
{
MyClass p = new MyClass(); // Map the delegate to the instance method:
MyDelegate d = new MyDelegate(p.InstanceMethod);
d(); // Map to the static method:
d = new MyDelegate(MyClass.StaticMethod);
d();
}
}
public class aaa
{
public void hx(ref string str_tem)
{
这里是一个接收串口数据,想在这里将接收到的数据传回到主程序里
string str_aa = "aa";
str_tem = str_aa;
}
}
在主程序中,只要调用时
aaa bb = new aaa();
string str_tem = "";
bb.hx(ref str_tem);
{
string a=这里是一个接收串口数据,想在这里将接收到的数据传回到主程序里
return a;
}
多数情况下,不要让aaa去操作主程序。
public class aaa
{
public event EventHandler HxExecuted;
public void hx()
{
if ( HxExecuted != null ){
HxExecuted( this, EventArgs.Empty ); //通知挂接到事件链上的调用者。
}
这里是一个接收串口数据,想在这里将接收到的数据传回到主程序里
}
}主程序
aaa a = new aaa();
a.HxExecuted += new EventHandler( this.aaa_HxExecuted );private void aaa_HxExecuted( object sender, EventArg e ) {
// 得到a的Hx方法执行的消息,可以处理了.
}
UP UP
UP UP UP
需要通知时触发一下。
事件和委托参考
http://blog.csdn.net/zhzuo/archive/2004/06/15/22038.aspx
http://blog.csdn.net/zhzuo/archive/2004/06/15/22039.aspx
如果需要可以留言。