Momo Pomo
Momo Pomo

Reputation: 265

How to dynamically add values in a 2D array?

I am working with 2D arrays. what I want, is to dynamically add elements in specific columns of my 2D array named symboltable2. I have been doing it as;

result is another 1D array in which there are certain words:

string[,] symboltable2 = new string[,];

if (result.Contains("int")) {
    for (int todynamic = 0; todynamic < result.GetLength(0); todynamic++) {
        symboltable2[todynamic, 6] = "int";
    }
    for (int sym2 = 0; i < symboltable1.GetLength(0); sym2++) {
        f4.listBox6.Items.Add(symboltable1[sym2, 5]); // To show if the values are added or not
    }
} 

but the code above isn't giving me any results... kindly help :(

Upvotes: 3

Views: 34877

Answers (2)

Paul Sasik
Paul Sasik

Reputation: 81429

You need to set the size of the array. And to have it public I would use a property and initialize your array in the class constructor like this:

public class MyClass
{
    public string[,] symboltable2 { get; set; } 

    public MyClass()
    {
        symboltable2 = new string[10,10];
    }

            // ...

Upvotes: 2

sihirbazzz
sihirbazzz

Reputation: 718

while implementing arrays you need to give the array's dimension i.e.

string[,] sa = new string[5,15];

or

string[,] sa = new string[listString1.Count, listString2.Count] 

about adding / changing elements to 2D array.. as a simple string array example :

sa[0, 1] = "a";
sa[0, 2] = "b";
sa[1, 0] = "Istanbul / Turkey";

Upvotes: 2

Related Questions