Solved

Accessing sub list data in a C# struct

Posted on 2014-10-22
6
432 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What is Waterfall Model? Waterfall model is the classic Software Development Life Cycle method practiced in software development process. As the name "waterfall" describes, this development is flowing downwards steadily like waterfall, i.e., procee…
Dependencies in Software Design In software development, the idea of dependencies (http://en.wikipedia.org/wiki/Coupling_%28computer_programming%29) is an issue of some importance. This article seeks to explain what dependencies are and where they …
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now