0

I'm doing some work with an NI ADC. I'm currently reading in voltages from the AO and AI into List<dynamic> and I'm having some issues with console.writeLine.

I'm using the dynamic type because the program needs to decide what the data should be stored at, at runtime instead of compile time.

So, because of this whenever I want to print the contents of the list, it doesn't know what i'm asking so it returns the type being stored, not the selected element data.

public void createTask(DataGrid grid, List<Object> data, float sampleRate, int sampleAmount, ComboBox channel, float minRange, float maxRange)
        {
            using (Task myTask = new Task())
            {
                myTask.AIChannels.CreateVoltageChannel(channel.Text, "",
                    (AITerminalConfiguration)(-1), minRange, maxRange, AIVoltageUnits.Volts); // create the task to measure volts

                myTask.Timing.ConfigureSampleClock("", sampleRate, SampleClockActiveEdge.Rising, // create the timing
                    SampleQuantityMode.ContinuousSamples, sampleAmount);

                AnalogMultiChannelReader reader = new AnalogMultiChannelReader(myTask.Stream);

                myTask.Control(TaskAction.Verify);

                data.Add(reader.ReadSingleSample());

                Console.WriteLine(data[0]);
            }
        }

Which in turn prints out System.Double[]. How would I go about printing out what the element actually stores rather then its type? I've tried lots of different ways of trying to get what I'm after but I'm struggling with C# syntax (I use C++) - only been using it for three weeks.

I've tried;

  • Casting
  • ToString() Conversion
  • Copying the contents of the whole list to a <double> list with CopyTo.

I'm at a bit of a loss here.

Johnathan Brown
  • 713
  • 12
  • 35
  • Usually your object just has to override the `ToString()` method to provide a meaningful string result. However in this case, it looks like you also store _collections_ (specifically, arrays) of these objects. I would suggest you pass the printing work to a utility method that checks if the object implements `IEnumerable`, and if so, iterate on it printing each element rather than printing the collection. – Chris Sinclair Jun 26 '14 at 13:41
  • Interesting, though I'm not sure I completely understand exactly what you just said. Could you explain to me what a utility method is and what you mean by checking if the object implements `IEnumerable` – Johnathan Brown Jun 26 '14 at 13:55
  • dynamic is a keyword in C#, and it is not equivalent to object. http://msdn.microsoft.com/en-ca/library/dd264736.aspx – Matt Jun 26 '14 at 14:25
  • what is the return type of reader.ReadSingleSample()? double[]? – Matt Jun 26 '14 at 14:26

3 Answers3

1

1)You can iterate over the array

 double[] data = new double[] { 1, 2, 3 };
 foreach (var item in data)
       Console.WriteLine(item.ToString());

2)Or

 Console.WriteLine(string.Join(",", data)); 

The above solutions work if you already have an array of double. In your case ,if you are sure that data[0] is an array of double you can use the as operator

double[] temp = data[0] as double[];
Console.WriteLine(string.Join(",", temp));
George Vovos
  • 7,563
  • 2
  • 22
  • 45
  • Thanks for the cool foreach statement, though if I wanted to loop / iterate over my List collection I could off, this will still print out the list collection type rather then the data held in the elements. – Johnathan Brown Jun 26 '14 at 13:53
  • Ah you legend. Quick question so I fully understand your code. The `as double[];` am I correct in thinking that is converting the element `[0]` held in my List collection to a double? The `string.Join` what does this function actually do? I've never seen it before. Thanks – Johnathan Brown Jun 26 '14 at 14:22
  • Yes the as operator casts the object to the specified type. The string.Join method takes 2 arguments a)a separator b)an array of objects and returns 1 string that is the array values separated by the separator.In my example 1,2,3 – George Vovos Jun 26 '14 at 14:24
1

Your List<object> data must be not empty before you call your method. If you do Console.WriteLine(data.Last()); instead of Console.WriteLine(data[0]), you will surely get the result

Perfect28
  • 11,089
  • 3
  • 25
  • 45
  • This will print out the collection type instead of the actual data, just another way of doing the same thing. My `Listdata` collection currently holds one element (A voltage) – Johnathan Brown Jun 26 '14 at 13:52
1

So the first item in your list in an array of double. If you want to see the values from that array printed out, you can manually iterate through them, or you can use the following code to print out array items if the object is an array, otherwise just print the item value.

        if (data[0].GetType().IsArray)
        {
            Console.WriteLine(string.Join(",", ((Array)data[0]).Cast<object>()));
        }
        else
        {
            Console.WriteLine(data[0]);
        }
Grax32
  • 3,986
  • 1
  • 17
  • 32