Link to home
Start Free TrialLog in
Avatar of snieves
snieves

asked on

Accessing sub list data in a C# struct

I am trying to access data that is a sub list within a  datalist structure. The structure is as follows:

Field1
Field2
Field3
Field4
rawDataList (data I need to grab)
   0
     SubField1
       0 - Value I need to get
       1 - Value I need to get
       2 - Value I need to get
     Subfield2
     Subfield3
       0 - Value I need to get
       1 - Value I need to get
   1
     SubField1
       0 - Value I need to get
       1 - Value I need to get
       2 - Value I need to get
     Subfield2
     Subfield3
       0 - Value I need to get
       1 - Value I need to get
Field5

The following is what I thought would work but I get an error:

DateTime rTime;
            float rAmbient;
            float rTemp;
            float rVolt;

            for (int i = 0; i <= count; i++)
            {
                

                unsafe
                {
                    for (int t = 0; t <= 31; t++)
                    {
                        rTemp = _dataBase.rawDataList[i].temp[t];
                        rAmbient = _dataBase.rawDataList[i].tempAmbient;
                        rTime = _dataBase.rawDataList[i].time;
                        rVolt = _dataBase.rawDataList[i].v[t];
                      
                        rTable.Rows.Add(rTemp, rAmbient, rTime, rVolt);
                    }
                }     
            }

Open in new window


This is the error I get: Fixed size buffers can only be accessed through locals or fields

VS2013 directs me to MSDN but I cannot figure out how to fix the error. The error is with the temp and v values. Any  help or direction would be greatly appreciated...
Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

Is it a requirement that they are fixed arrays in the first place?
Why not just make v and temp ArrayLists and set the size in the constructor to whatever you want it to be?
Why are you using the unsafe modifier?
It looks like you are trying to use an array because you used them in C++ or C. I'm guessing you added 'unsafe' because it made you do that to use the fixed arrays. You could probably also just use dynamic arrays. Use something like this to declare it
int[] temperature;

Open in new window

and something like this in the constructor
temperature = new int[31];

Open in new window


If you need more help, post the definition of the class.

Also note that your inner for loop is going to run 32 times. Is this intentional? I'm guessing it's for days in a month and you want < 31, not <= 31?
Avatar of snieves
snieves

ASKER

It doesn't have to be in an array. The MSDN article I referenced said to create a variable and assign the in-line array to the variable. I did that and that is when I got the fixed size buffer error. The program is testing 32 modules for temp and voltage every second for a variable amount of time. I need to grab the data, the in-line array dataset and push that to SQL. The only part I'm having an issue with is getting the in-line array data. I did not generate the dataset, a different programmer handled that. Here is my code so far.

private void dbThread()
        {
            int count = _dataBase.rawDataList.Count();

            var timeStamp = DateTime.Now;
            var tID = Guid.NewGuid().ToString("N");

            // Summary data table //
            DataTable table = new DataTable();
            table.Columns.Add("ahDch", typeof(float));
            table.Columns.Add("moduleSN1", typeof(string));
            table.Columns.Add("moduleSN2", typeof(string));
            table.Columns.Add("newPackSN", typeof(string));
            table.Columns.Add("testType", typeof(string));
            table.Columns.Add("timeStamp", typeof(DateTime));
            table.Columns.Add("tID", typeof(string));
            table.Columns.Add("tIndex", typeof(string));

            for (int i = 0; i <= 31; i++)
            {
                table.Rows.Add(_dataBase.ahDch[i], _dataBase.moduleSN1[i], _dataBase.moduleSN2[i], _dataBase.newPackSN.Substring(36), _dataBase.testType, timeStamp, tID, i);
            }

            // Detail data table //
            DataTable rTable = new DataTable();
            rTable.Columns.Add("temp", typeof(float));
            rTable.Columns.Add("tempAmbient", typeof(float));
            rTable.Columns.Add("time", typeof(DateTime));
            rTable.Columns.Add("v", typeof(float));
            rTable.Columns.Add("timeStamp", typeof(DateTime));
            rTable.Columns.Add("tiD", typeof(string));
            rTable.Columns.Add("tIndex", typeof(string));

            
            float rTemp = 0;
            float rAmbient;
            DateTime rTime;
            float rVolt = 0;

            try
            {
                for (int i = 0; i <= count; i++)
                {
                    rAmbient = _dataBase.rawDataList[i].tempAmbient;
                    rTime = _dataBase.rawDataList[i].time;
                    ArrayList myArr = new ArrayList {_dataBase.rawDataList[i]};

                    unsafe
                    {
                        for (int t = 0; t <= 31; t++)
                        {
                            rTemp = myArr.temp[t]; // Does not contain a definition for temp
                            rVolt = myArr.v[t]; // Does not contain a definition for v
                            
                            rTable.Rows.Add(rTemp, rAmbient, rTime, rVolt, timeStamp, tID, i);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            
            string connectionString = "connection string";

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();

                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
                {
                    bulkCopy.DestinationTableName = "dbo.Table1";

                    try
                    {
                         bulkCopy.WriteToServer(table);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                }

                using (SqlBulkCopy bulkCopy2 = new SqlBulkCopy(connection))
                {
                    bulkCopy2.DestinationTableName = "dbo.Table2_Items";

                    try
                    {
                        bulkCopy2.WriteToServer(rTable);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                }
            }

Open in new window


Thank you so much for your help!!!
ASKER CERTIFIED SOLUTION
Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of snieves

ASKER

Sorry for the delay. Worked like a charm. Thank you so much!!!