Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I implement an unknown Array type in C# .Net Core?

I have the following code below but haven’t figured out how to implement the
var results = section so that result[c] = valuesArray; works properly?

    var result = Array.CreateInstance(typeof(Array), dt.Columns.Count);

            foreach (int c in Enumerable.Range(0, dt.Columns.Count))
            {
                // Create a list to hold values of the required type for the column
                var valuesArray = Array.CreateInstance(typeof(float), dt.Rows.Count);

                // Get the data to be written to the parquet stream
                for (int r = 0; r < dt.Rows.Count; r++)
                {
                    DataRow row = dt.Rows[r];

                    valuesArray.SetValue(row[c], r);
                }

                result[c] = valuesArray;

The error is:

Error: CS0021 = Cannot apply indexing with [] to an expression of type 'Array'

How do I instiate the Array so it works?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just as you set a value to valuesArray with the SetValue() method, you can do the same for result:

result.SetValue(valuesArray, c);

If you need to retrieve the value during iteration, you can use result.GetValue(c):

foreach (int c in result)
{
    var retrievedResult = result.GetValue(c);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading