2

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?

0

5 Answers 5

3

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); 
 }
Sign up to request clarification or add additional context in comments.

6 Comments

@Cyral: Thanks for your Kind support :)
I follow your codes, but now I have different error message at the same line. Error: use of unsigned local variable. any idea please....:(
@Ren please see my answer below. Thanks! please don't forget to accept and upvote the correct answers.
@SudhakarTillapudi It still prompt "input string not in correct format". Maybe it does not resize the GlobalDataClass array..correct me if im wrong...=(
@Ren: assign it to null before assigning size to GlobalDataClass.dDataArray as GlobalDataClass.dDataArray=null;
|
2

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.

6 Comments

Wow,you even correct my mistakes. There's no error now, but the code resize doesn't work. It still prompt input string not in correct format whenever i load text file which have less length then my global class array..do you have any solution please?
@Ren: you need to add this line -> GlobalDataClass.dDataArray=new double[line.Length,2]; after arrayTemp declaration, check my edited answer.
@Ren please follow EDIT 2 in my answer
@WasifHossain I still have the same error: " input string not in correct format"..any idea please?
@SudhakarTillapudi thanks. But even if I add as you said. It still prompt "input string not in correct format"..
|
2

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.

Comments

1

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

Comments

0

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!

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.