日期:2014-05-17  浏览次数:20638 次

C# 调用问题
Mainform.cs中 
 public static string isstr = "";
  void spPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            int DataLength = spPort.BytesToRead;
            int i = 0;
            StringBuilder sb = new StringBuilder();
            while (i < DataLength)
            {
                byte[] ds = new byte[1024];
                int len = spPort.Read(ds, 0, 1024);
                sb.Append(Encoding.ASCII.GetString(ds, 0, len));
                i += len;
            }
           
            isstr = sb.ToString();
          }
protocol.cs中
    internal void SetUserTime(string str_time)
        {
            str_time = MainFrom.isstr;   //我想调过isstr中的数据,但是不行,显示里面是空的       
            if (str_time.Length != 26)
            {
                return;
            }        
            else
            {
            UserV = str_time.Substring(str_time.Length - 7, 2).ToString() + "."
               + str_time.Substring(str_time.Length - 5, 2).ToString();
            }


        }
大家给看看,怎么把isstr中的数据调过来
c#

------解决方案--------------------
断点一下 , 看程序运行的先后顺序 
------解决方案--------------------
创建对象是最简单的办法
可以把Mainform  当参数
Mainform mf;
public protocol(Mainform mf)
{
    this.mf = mf;
}
public void SetUserTime()
{
 if (mf.str_time.Length != 26)
}
另外最好不要用static
------解决方案--------------------
定义的变量是静