0

I have two list that I convert them to a single two dimesnional array like this:

double[,] data = new double[voltage.Count(), 2];
for (int i = 0; i < voltage.Count(); i++)
{
    data[i, 0] = voltage[i];
    data[i, 1] = current[i];
}

Now I am trying to itterate through this array but what I get is same value for both voltage and current in each line:

foreach (double data in ztr.GetCurveDataForTestType()) //this will return my array
{
    richTextBox1.AppendText("Voltage" + data + "    ---------    ");
    richTextBox1.AppendText("Current" + data + "\r\n");
}

Voltage-0.175 --------- Current-0.175
Voltage-9.930625E-06 --------- Current-9.930625E-06
Voltage-0.171875 --------- Current-0.171875
Voltage-9.53375E-06 --------- Current-9.53375E-06
Voltage-0.16875 --------- Current-0.16875

As you see in the first line both voltage and current are same value that is voltage, and on the second raw they are the same again but this time it is the current value. How can I fix this?

4
  • Please show the code you use to print. Also, if you can show the values in the arrays, that might be helpful as well. Commented Mar 19, 2011 at 11:04
  • Use a for as you use for fill. Commented Mar 19, 2011 at 11:06
  • It certainly looks like GetCurveDataForTestType is returning a one dimensional array. Post all the code please. Commented Mar 19, 2011 at 11:08
  • 1
    Please show also the code you use to initialize both current and voltage arrays. Commented Mar 19, 2011 at 11:08

3 Answers 3

2

I'd recommend not using multi-dimensional arrays for this.

Instead you can make a class something like this:

class Measurement
{
    public double Voltage { get; set; }
    public double Current { get; set; }
}

And change your method to return IEnumerable<Measurement>. In .NET 4.0 you can use Zip:

public IEnumerable<Measurement> GetCurveDataForTestType()
{
    return voltage.Zip(current,
        (v, c) => new Measurement { Voltage = v, Current = c});
}

And for older versions of .NET:

public IEnumerable<Measurement> GetCurveDataForTestType()
{
    for (int i = 0; i < voltage.Count(); i++)
    {
        yield return new Measurement
        {
            Voltage = voltage[i],
            Current = current[i]
        };
    }
}

Then your code becomes:

foreach (Measurement data in ztr.GetCurveDataForTestType())
{
    richTextBox1.AppendText(
        "Voltage: {0} ---------  Current: {1}", data.Voltage, data.Current);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Sounds like a good idea, would you mind a little more info please?
@Sean87: It would help if you can provide the version number of the .NET framework and/or C# version you are using.
Thanks for extra code, I am doing this with .NET 4 and C# Express 2010
But I am already inside a class I prefebaly dont want to make a new class for only this method. is there a way?
@Sean87: There's no harm in having a class that is only used in one or two methods - you can make it an inner class if you want to avoid cluttering the namespace. Alternatively you can use an anonymous type but then you cannot (easily) return it from a method. Or you can use Tuple<double, double> instead of a custom class but then you have to refer to the members by index again.
1

You use the same kind of loop as when you created the array:

for (int i = 0; i < data.GetLength(0); i++) {
  richTextBox1.AppendText("Voltage" + data[i, 0] + "    ---------    ");
  richTextBox1.AppendText("Current" + data[i, 1] + "\r\n");
}

1 Comment

Thanks this sovled my problem at the moment but I want to try that class method.
0

Update: This question appears to be a duplicate of How to foreach through a 2 dimensional array?. The linked answer has an alternate solution that enables the use of foreach by switching from a two-dimensional array to an array of array.


foreach with a two dimensional array, will sequentially return every double value in the array (by design).

For example, given the following two-dimensional array of doubles:

var array = new double[,] { { 0, 1, }, { 2, 3 }, { 4, 5 } };

foreach (var d in array)
{
    Console.WriteLine(d);
}

You will get the following Console output:

0
1
2
3
4
5

Therefore, foreach is not appropriate for you needs. You should use a for loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.