Ren
Ren

Reputation: 775

Copy 2D Array in C#

I have a code that stored the data to a temporary array.

string filename = openFileDialog1.FileName;
string[] line = File.ReadAllLines(filename);
using (var reader2 = File.OpenText(@filename))
{
    for (int i = 0; i < line.Length; i++)
    {
        string lines = reader2.ReadLine();
        var data = lines.Split(',');
        double[,] arrayTemp = new double[line.Length, 2];
        arrayTemp[i, 0] = double.Parse(data[0]);
        arrayTemp[i, 1] = double.Parse(data[1]);
    }
    Array.Copy(arrayTemp, GlobalDataClass.dDataArray, line.Length); //error the name "arrayTemp" does not exist in the current context.
} 

Since 2d array is not resizable and I want my global array to be flexible,so I use the Array.Copy method to copy the temp array to the Global class array. However I got an error as commented in my code above.

My question is how to copy the tempArray to the global class array. Any idea how to fix this?

Upvotes: 2

Views: 2818

Answers (5)

I have answered on this link

To copy an array to another one, just use the "Clone()" function like the following:

This is your array list

object newArray = new object [row, column];

When you are creating another Array just use this code:

object[,] clonedArray = (object[,]) newArray.Clone();

Simple! Have fun!

Upvotes: 0

Wasif Hossain
Wasif Hossain

Reputation: 3950

EDIT:

it seems that you are trying to copy the contents of arrayTemp to GlobalDataClass.dDataArray, but you are assigning values to GlobalDataClass.dDataArray, and trying to copy empty arrayTemp to GlobalDataClass.dDataArray.

So first declare the tempArray outside the for-loop and then populate it instead of GlobalDataClass.dDataArray inside the for-loop accordingly:

string filename = openFileDialog1.FileName;
string[] line = File.ReadAllLines(filename);

var arrayTemp = new double[line.Length, 2];

using (var reader2 = File.OpenText(@filename))
{
    for (int i = 0; i < line.Length; i++)
    {
        string lines = reader2.ReadLine();
        var data = lines.Split(',');
        arrayTemp[i, 0] = double.Parse(data[0]);
        arrayTemp[i, 1] = double.Parse(data[1]);
    }
    Array.Copy(arrayTemp, GlobalDataClass.dDataArray, line.Length); // now the error should go away.
}

EDIT 2:

you don't need to read the file 2nd time in the using() clause.

Now please try the following:

string filename = openFileDialog1.FileName;
string[] line = File.ReadAllLines(filename);

var arrayTemp = new double[line.Length, 2];

for (int i = 0; i < line.Length; i++)
{
    var data = line[i].Split(',');
    arrayTemp[i, 0] = double.Parse(data[0]);
    arrayTemp[i, 1] = double.Parse(data[1]);
}
Array.Copy(arrayTemp, GlobalDataClass.dDataArray, line.Length); // now the error should go away.

Upvotes: 2

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Problem : You have declared your array variable arrayTemp inside the for loop and it is not available outside of it.

Solution : You need to move your array variable arrayTemp declaration outside the loop.

Try This:

 string filename = openFileDialog1.FileName;
 string[] line = File.ReadAllLines(filename);
 double arrayTemp=new double[line.Length,2];//declare outside forloop so it available after forloop.
 GlobalDataClass.dDataArray=new double[line.Length,2]; //add this line
 using (var reader2 = File.OpenText(@filename))
 {
   for (int i = 0; i < line.Length; i++)
   {
     string lines = reader2.ReadLine();
     var data = lines.Split(',');         
     GlobalDataClass.dDataArray[i, 0] = double.Parse(data[0]);
     GlobalDataClass.dDataArray[i, 1] = double.Parse(data[1]);
   }
    Array.Copy(arrayTemp, GlobalDataClass.dDataArray, line.Length); 
 }

Upvotes: 3

Cyral
Cyral

Reputation: 14153

Your tempArray is out of the scope of you Array.Copy because it is inside of your for loop, you need to move it into the scope of your method call so the method can access it. (Similar to how GlobalDataClass.dDataArray is declared elsewhere)

//Array is now declared in an accessible scop
double[,] arrayTemp;
string filename = openFileDialog1.FileName;
string[] line = File.ReadAllLines(filename);
using (var reader2 = File.OpenText(@filename))
{
    for (int i = 0; i < line.Length; i++)
    {
        string lines = reader2.ReadLine();
        arrayTemp = new double[line.Length, 2];
        var data = lines.Split(',');
        GlobalDataClass.dDataArray[i, 0] = double.Parse(data[0]);
        GlobalDataClass.dDataArray[i, 1] = double.Parse(data[1]);
    }
    Array.Copy(arrayTemp, GlobalDataClass.dDataArray, line.Length); //error the name "arrayTemp" does not exist in the current context.
} 

Statements "deeper" into the { } hierarchy are only accessible to statements within them, and other child scopes in them. Parent scopes outside of the original brackets cannot access the variables.

Upvotes: 2

Baahubali
Baahubali

Reputation: 4802

looks like you array is out of scope because it's declared inside the for loop and you are trying to access it outside the for loop

Upvotes: 1

Related Questions