Question

I have php code as below

   $array[$x] = array();
   $array[$x]["no"] = $no;
   $array[$x]["cid"] = $cid;
   $array[$x]["wait"] = $wait;
   $array[$x]["prio"] = $prio;
   $array[$x]["debug"] = $command[$i];

x++;

Question: I want to have similar code like above in C#, all is well but at the jagged array step, I am confused how to do same like this in C#, Can anyone help me out in this regard? Thats is index of an array is string value.

Was it helpful?

Solution

One way of achieving a jagged array is a dictionary. Something based on the code below may get you started.

Dictionary<string, string>[] array;

void MyMethod(int[] ckeys, int gotovalue, string[] command)
{
    int x = 0;
    for(int ii = (ckeys[0] + 1); ii < gotovalue; ii++)
    {
        string no = preg_replace(" .*", "", command[ii]); 
        string temp = preg_replace("^[0-9]*. ", "", command[ii]); 
        string cid = preg_replace(" (.*", "", temp); 
        temp = preg_replace(".* (wait: ", "", command[ii]); 
        string wait = preg_replace(",.*", "", temp); 
        temp = preg_replace(".*, prio: ", "", command[ii]); 
        string prio = preg_replace(").*", "", temp);

        array[x] = new Dictionary<string, string>();
        array[x]["no"] = no;
        array[x]["cid"] = cid;
        array[x]["wait"] = wait;
        array[x]["prio"] = prio;
        array[x]["debug"] = command[ii];

        x++;
    }
}

string preg_replace(string aa, string bb, string cc)
{
    return aa + bb + cc;
}

Edit:

I took the code in the initial version of the question and tried to convert it into C#, assuming that all the unspecified types were strings. The called routine preg_replace was not specified, but it appeared to take three strings and return one.

The original question has the line $x = 0; which appears to define $x as an integer and initialize it. The line $array[$x] = array(); appears to say that $array at the given integer index is made to refer to an empty array. Then the line $array[$x]["no"] sets the "no" element of that array to a string. The C# I proposed declares array as an array of dictionaries. A C# dictionary is a form of associative array, in the Perl language it would be called a 'hash'. The whole piece of code will write values into the structure, effectively initializing it from the values found in the parameters to MyMethod.

Elsewhere will need a statement such as array = new Dictionary<string, string>[gotovalue] to make array refer to an actual array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top