3

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 :(

3
  • string[,] symboltable2= new string[,] won't even compile: Array creation must have array size or array initializer Commented May 27, 2012 at 16:41
  • so wot is the proper way to declare it? Commented May 27, 2012 at 16:43
  • as i hav to keep my symboltable2 public Commented May 27, 2012 at 16:44

2 Answers 2

2

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

Comments

2

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];
    }

            // ...

2 Comments

+1. If possible define it as public string[,] SymbolTable2 { get; private set; } using a private setter.
This will just the same in WinForms. Classes are classes and fields are fields.

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.