如何在 C# 中将结构体数组传递给类

发布于 2024-12-11 09:04:34 字数 1631 浏览 0 评论 0原文

总之,我很难将 struct 传递给类构造函数。我正在对现有代码的一部分进行多线程处理。在一个类中定义了以下结构:

SchemaFileRec[] m_TableList;

struct SchemaFileRec
{
    public ArrayList  saFile;
    public ArrayList  saImpTab;
    public ArrayList  saSheet;
    // And others...
    public int[]      nColHead;
    public int[]      nSkipBeg;
    public int[]      nSkipEnd;
    public string     strADTFld;
};

这在方法(称为 ExecCmd 的旧方法)中使用来进行一些处理。我正在将这个方法重写为一个类;实际上是BackgroundWorker 的子类。本质上,我正在采用一种处理某些“长时间运行”任务的方法,并对它进行多线程处理。我有:

public class ExecCmdsThredded : BackgroundWorker 
{
    // Global variables and constants.
    private int nTable;
    private List<int> kPrmArray = null;
    private List<object> StructureArray = null;
    private SpreadsheetGear.IWorksheet worksheet;

    // Default constructor to handle thred properties.
    public ExecCmdsThredded()
    {
        WorkerReportsProgress = true;
        WorkerSupportsCancellation = true;
    }

    // Constructor to parse relevant parameters to worker.
    // Or as an array of objects public ExecCmdsThredded(object[] _StructArr, etc. 
    public ExecCmdsThredded(List<object> _StructArr, List<int> _kPrmArr, ArrayList[] _saCmd, 
                            int _nTable, SpreadsheetGear.IWorksheet _worksheet) : this()
    {
        this.nTable = _nTable;
        this.kPrmArray = _kPrmArr;
        this.StructureArray = _StructArr;
        this.worksheet = _worksheet;
    } 
    // ...

我现在想通过构造函数将结构从上面传递到 ExecCmdsThredded 类,但我以前从未这样做过,而且我不确定是否可以按照我想要的方式进行?我可以显式传递 struct 的每个组件,但我想避免这种情况。

感谢您抽出时间。

All, I am having difficulty passing a struct to a class constructor. I am in the process of multi-threading a part of an existing code. In one class the following structure is defined:

SchemaFileRec[] m_TableList;

struct SchemaFileRec
{
    public ArrayList  saFile;
    public ArrayList  saImpTab;
    public ArrayList  saSheet;
    // And others...
    public int[]      nColHead;
    public int[]      nSkipBeg;
    public int[]      nSkipEnd;
    public string     strADTFld;
};

This is used in a method (old method called ExecCmd) to do some processing. I am re-writing this method to a class; in fact a subclass of BackgroundWorker. Essentally I am taking a method that deals with some "long-running" task, and multi-thredding it. I have:

public class ExecCmdsThredded : BackgroundWorker 
{
    // Global variables and constants.
    private int nTable;
    private List<int> kPrmArray = null;
    private List<object> StructureArray = null;
    private SpreadsheetGear.IWorksheet worksheet;

    // Default constructor to handle thred properties.
    public ExecCmdsThredded()
    {
        WorkerReportsProgress = true;
        WorkerSupportsCancellation = true;
    }

    // Constructor to parse relevant parameters to worker.
    // Or as an array of objects public ExecCmdsThredded(object[] _StructArr, etc. 
    public ExecCmdsThredded(List<object> _StructArr, List<int> _kPrmArr, ArrayList[] _saCmd, 
                            int _nTable, SpreadsheetGear.IWorksheet _worksheet) : this()
    {
        this.nTable = _nTable;
        this.kPrmArray = _kPrmArr;
        this.StructureArray = _StructArr;
        this.worksheet = _worksheet;
    } 
    // ...

I now want to pass the structure from above to ExecCmdsThredded class via the constructor but I have never had to do this before and I am not sure if it is possible the way I want to do it? I could explicitly pass every componant of the struct but I want to avoid this.

Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

早茶月光 2024-12-18 09:04:34

您始终可以传递对象数组 object[] ,并且在内部可以装箱任何类型的结构或类,但不建议这样做,因为装箱/拆箱副作用。我的意思的示例:

new ExecCmdsThredded(new object[] { new SchemaFileRec(), new AnotherSchema() });

但我建议删除结构,创建类,以便您可以使用泛型来代替,至少使代码更具可读性。

You can always pass an array of objects object[] and inside you can box any type of struct or class, it is not advised because of the boxing/unboxing side effects. Example of what I mean:

new ExecCmdsThredded(new object[] { new SchemaFileRec(), new AnotherSchema() });

But I recommend dropping the structs, create classes so that you can use Generics instead, makes the code more readable at least.

掀纱窥君容 2024-12-18 09:04:34

每当我开始传递结构时,我都会感到不舒服。当您拥有想要作为参数传递的结构体集合时,请仔细重新审视设计......“为什么我要使用结构体?”我建议在大多数情况下将结构转换为类。

有几个链接可以帮助您决定是使用结构体还是类。搜索 StackOverflowMSDN

I feel uncomfortable whenever I start passing structs. When you have a collection of structs that you want to pass as a parameter, carefully revisit the design ..."why am I using a struct?" I recommend converting the struct to a class in most cases.

There are several links to help you decide on whether to use a struct or class. Search through StackOverflow and MSDN

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文