-2

I tried to move contains of an array from a form to another but I could'nt. What I coded is that.

First form:

// (This array has got something inside of it)
public string[,] yeniKayitlar = new string[100, 7];  

Second form:

bilgiEkle be = new bilgiEkle();

string[,] kayitlar = new string[100, 7];

private void anaEkran_Load(object sender, EventArgs e) {

    be.Hide();

    for (int i = 0; i < kayitlar.GetLength(0); i++) {

        for (int a = 0; a < kayitlar.GetLength(1); a++) {
            kayitlar[i, a] = be.yeniKayitlar[i, a];
        }
    }
}

As you see, I could'nt to move. Can you help me?

stuartd
  • 70,509
  • 14
  • 132
  • 163
2000kadir
  • 3
  • 1
  • 1
    Could you show your code in English, pls? – Serge Jan 27 '21 at 18:11
  • Why doesn't it moves? What error are you getting? – Alejandro Jan 27 '21 at 18:22
  • In the posted code… `bilgiEkle be = new bilgiEkle();`… is never “Shown,” at least I do not see where `be.Show()` is executed. So, `be.Hide()` accomplished nothing and most likely `be.yeniKayitlar` is not filled with data. Can you explain “why” `bilgiEkle` is created but never shown? Put a breakpoint on the line… `kayitlar[i, a] = be.yeniKayitlar[i, a];` and make sure `be.yeniKayitlar` contains data. I am betting it does not have any data. – JohnG Jan 27 '21 at 18:31
  • Fundamentally, it appears your problem is that you are creating a _new instance_ of the first form, instead of using an _already existing instance_ of it. This results in a completely different instance of the array you're copying from. Beyond that though, you should not be attempting this kind of direct interaction between the forms in the first place. See duplicate for more specific details about how to allow two forms to exchange data with each other. – Peter Duniho Jan 27 '21 at 18:36
  • 2
    Also, your question here is very poorly presented. You write _"As you see, I could'nt to move"_, but no...we _can't_ "see". We can make an educated guess about what you've done wrong, but lacking a proper [mcve], and lacking a clear explanation of what the code does and how that's different from what you want, it's not literally possible to know for sure what the problem is. See [ask] for information on how to present your question in a clear, answerable way. – Peter Duniho Jan 27 '21 at 18:38

2 Answers2

-1

Array.Copy is the method to use, without having to loop through all the contents of the array.

An example of this would be Array.Copy(kayitlar, yeniKayitlar, kayitlar.Length), which copies all the contents of the kayitlar to yeniKayitlar array.

Dash
  • 306
  • 1
  • 3
  • 16
-1

You can use with Array.Copy(numbers1, numbers2, 9); this method.

    int[] numbers1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int[] numbers2 = new int[9];
    
    Array.Copy(numbers1, numbers2, 9);

    foreach (int item in numbers2)
    {
        Console.WriteLine(item);
    }
   
Burakkepuc
  • 45
  • 1
  • 6