Solved

Accessing sub list data in a C# struct

Posted on 2014-10-22
6
439 Views
Last Modified: 2014-10-31
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
Comment
Question by:snieves
  • 3
  • 2
6 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 40399143
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40399263
Why are you using the unsafe modifier?
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 40399719
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 1

Author Comment

by:snieves
ID: 40399816
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
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 40399917
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
 
LVL 1

Author Closing Comment

by:snieves
ID: 40415731
Sorry for the delay. Worked like a charm. Thank you so much!!!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question