我在一个C#类中有一个结构数组,我想将该数据传递到另一个类中。
这是我的代码
private struct strClassOfService
{
public string COS;
public string myValue;
}
private strClassOfService[] arrCOS = null;
//
// some code that loads arrCOS with data
//
//
// here is where i will instantiate another class and
// set the arrCOS so I can use it in the other class
//
Run Code Online (Sandbox Code Playgroud)
如果所有其他方法都失败了,我想我可以在另一个类中重新加载数据。但是我很好奇是否有办法。到目前为止,我的尝试失败了。
好吧,一方面,如果您打算将结构传递给另一个类,则应该将结构定义公开(或至少是内部)。一旦这样做,就可以使用各种方法(属性,方法呼叫等)将数据复制到您的其他班级。
下面显示了两种技术(当然,您只需要使用一种...)
public class Foo1
{
public struct Bar
{
string A;
string B;
}
private Bar[] data;
// Using a method
public Bar[] ExportData()
{
return data;
}
// Using properties
public Bar[] DataProperty
{
get { return data; }
}
}
public class Foo2
{
private Foo1.Bar[] data;
// Using a method
public void ImportData(Foo1 source)
{
this.data = source.ExportData();
}
// Using properties
public Foo1.Bar[] DataProperty
{
get { return data; }
}
public void ReadProperty(Foo1 source)
{
this.DataProperty = source.DataProperty;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8668 次 |
| 最近记录: |