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.
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