C# 从另一个类中的数组读取数据

C# 从另一个类中的数组读取数据,c#,C#,我需要从另一个类中的数组中读取数据。我已经读了几个线程来解释相同的问题,但无法让它与我的代码一起工作 包括的示例:我需要从Parsedata类到Form1类读取ParticipantX数组中的数据,如我的示例所示 如果有任何帮助,我将不胜感激。最好是你能帮我写下我需要的代码。我现在被卡住了。谢谢 public class Parsedata { public void ParsedataMethod() { ... //Neccesary data are

我需要从另一个类中的数组中读取数据。我已经读了几个线程来解释相同的问题,但无法让它与我的代码一起工作

包括的示例:我需要从
Parsedata
类到
Form1
类读取
ParticipantX
数组中的数据,如我的示例所示

如果有任何帮助,我将不胜感激。最好是你能帮我写下我需要的代码。我现在被卡住了。谢谢

public class Parsedata
{
    public void ParsedataMethod()
    {
    ...
        //Neccesary data are added to this array
        string[,] ParticipantX = new string[40, 4];
在同一命名空间中,我有Form1类:

using Crestron.ActiveCNX;
public partial class Form1 : Form
{
    ActiveCNX cnx;
    cnx = new ActiveCNX();

    private void SerialSend_Click(object sender, EventArgs e)
    {
        int number = 8;
        for (int i = 1; i < number; i++)
            cnx.SendSerial(i, ParticipantX[i, 1]); //
    }
使用Crestron.ActiveCNX;
公共部分类Form1:Form
{
ActiveCNX-cnx;
cnx=新的ActiveCNX();
私有void SerialSend\u单击(对象发送方,事件参数e)
{
整数=8;
对于(int i=1;i
您可以创建Parsedata和ParsedataMethod静态方法,也可以在表单中创建Parsedata的实例:

var pd = new ParseData();
然后您可以使用:

pd.Participant(...);
试试这个:

public class Parsedata
{
     private string[,] m_ParticipantX;
     public void ParsedataMethod()
     {
       ...
       m_ParticipantX = new string[40, 4];//Neccesary data are added to this array
     }

     public string[,] ParticipantX
     {
          get { return m_ParticipantX; }
     }
}

using Crestron.ActiveCNX;
public partial class Form1 : Form
{
    ActiveCNX cnx;
    cnx = new ActiveCNX();
    Parsedata data = new Parsedata();

    private void SerialSend_Click(object sender, EventArgs e)
    {
        data.ParseDataMethod();
        int number = 8;
        for (int i = 1; i < number; i++)
            cnx.SendSerial(i, data.ParticipantX[i, 1]); //
    }
}
公共类解析数据
{
私有字符串[,]m_ParticipantX;
public void ParsedataMethod()
{
...
m_ParticipantX=新字符串[40,4];//将必要的数据添加到此数组中
}
公共字符串[,]ParticipantX
{
获取{return m_ParticipantX;}
}
}
使用快思聪.ActiveCNX;
公共部分类Form1:Form
{
ActiveCNX-cnx;
cnx=新的ActiveCNX();
Parsedata数据=新的Parsedata();
私有void SerialSend\u单击(对象发送方,事件参数e)
{
data.ParseDataMethod();
整数=8;
对于(int i=1;i
方法中声明了
ParticipantX
数组,这意味着它将是该方法的本地数组

必须将声明放在方法之外:

public class Parsedata
{
    public string[,] ParticipantX;

    public void ParsedataMethod()
    {
        ...
        ParticipantX = new string[40, 4];
我已将
ParticipantX
标记为
public
,以便您可以从表单中访问它:

ParseData parseData = new ParseData();
parseData.ParticipantX[x, y] ...
public static event EventHandler<MyArgs> SendArray;
    public void ParsedataMethod()
    {
        string[,] ParticipantX = new string[40, 4];
        OnArraySend(new MyArgs() { myArray = ParticipantX });
    }

    protected virtual void OnArraySend(MyArgs ea)
    {
        if (SendArray != null)
        {
            SendArray(this, ea);
        }
    }
更好的方法是将其作为公共财产:

private string[,] _participantX;
public string [,] ParticipantX
{
    get { return _participantX; }
}

首先,请记住属性是类的一部分,并且类具有属性(或字段)。没有类所有者,您无法获取属性

在静态模式下,您可以实现以下功能:

public static class Parsedata{
  public static string[,] StringX{get;set;}
}
但是,如果类和属性不是静态的,则需要首先将类实例化为对象。例如:

public class Parsedata{
  public string[,] StringX{get;set;}
}
public class Caller{
  Parsedata p = new Parsedata();
  public void SetStringX(string[,] stringX){
    p.StringX = stringX;
  }
  public string[,] GetStringX(){
    return p.StringX;
  }
}
但是,您必须记住,实例化对象的值在实例之间是不同的。示例(仅说明):


您可以通过委托+事件处理程序将其传入,例如:

public partial class Form1 : Form
{
    public string[,] ParticipantX;
    public Form1()
    {
        InitializeComponent();
        Class1.SendArray += new EventHandler<MyArgs>(GetArray);
    }

    public void GetArray(object sender, MyArgs ea)
    {
        this.Invoke(new MethodInvoker(
            delegate()
            {
              ParticipantX = ea.myArray;
            }));
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Class1 t = new Class1();
        t.ParsedataMethod();
    }
}

public class MyArgs : EventArgs
{
    public string[,] myArray { get; set; }
}

这是一个粗略的示例,但是您得到了一般的想法

如果您的格式正确,那么ParticipantX是ParsedataMethod()的本地数组而且你不能访问局部变量。你能使它可能是静态的吗?我想重构你的代码将是一种比尝试访问方法局部变量更容易的解决方案。那些投票否决的人请详细说明-投票否决而不加评论,这样对任何人都没有帮助。有一个ParseDataMethod()在他的类ParseData中。您需要首先从ParseData类实例调用该方法,然后您的行pd.Participant(…)不正确,因为ParticipantX数组是在方法中声明的,而不是在类中声明的。这并没有解决变量是在方法体中声明的这一事实,这使得它首先是不可访问的。
public static event EventHandler<MyArgs> SendArray;
    public void ParsedataMethod()
    {
        string[,] ParticipantX = new string[40, 4];
        OnArraySend(new MyArgs() { myArray = ParticipantX });
    }

    protected virtual void OnArraySend(MyArgs ea)
    {
        if (SendArray != null)
        {
            SendArray(this, ea);
        }
    }