Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 512
  • Last Modified:

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...
0
snieves
Asked:
snieves
  • 3
  • 2
1 Solution
 
TommySzalapskiCommented:
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?
0
 
käµfm³d 👽Commented:
Why are you using the unsafe modifier?
0
 
TommySzalapskiCommented:
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?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
snievesAuthor Commented:
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!!!
0
 
TommySzalapskiCommented:
I was suggesting changing the actual temp array in the rawDataList to an ArrayList. But you say you can't change the definition of whatever type the rawDataList is? If you can't, then you'll have to work with it.

You didn't quite implement what the MSDN article suggested. You need something like this:

for (int t = 0; t <= 31; t++)
{
    WhateverTypeItIs temporaryVar =  _dataBase.rawDataList[i];
    rTemp = temporaryVar.temp[t];
}

Open in new window


At least, I think that's what you want. Without seeing the class that contains the fixed arrays, it's hard to be sure.
0
 
snievesAuthor Commented:
Sorry for the delay. Worked like a charm. Thank you so much!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now