-3

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.

Aemz
  • 137
  • 1
  • 2
  • 9
  • 1
    We don't know what your PHP is meant to do, which makes it very hard for anyone who doesn't know *both* PHP and C# to help you. – Jon Skeet Jun 02 '13 at 11:59
  • 3
    I believe PHP arrays are just hash tables, so it looks like you want a `Dictionary>` instead of an array in C#. – Lee Jun 02 '13 at 12:06

1 Answers1

2

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.

AdrianHHH
  • 13,492
  • 16
  • 50
  • 87
  • Is this really work?? I have tried it, but how can initialise an array like dictionary data type in c#? Thats not make senses? – Aemz Jun 02 '13 at 12:48
  • @Aemz I have added some explanations to my answer. Which aspect of the answer does not make sense? – AdrianHHH Jun 02 '13 at 15:51
  • it says Use of unassigned local variable 'array', how ever I have initialized same as you did after declaration of integer x? I think there is no initialization of array type in dictionary, that is why I said its not make sense. – Aemz Jun 02 '13 at 19:44
  • 1
    @Aemz my answer starts with the words "*Something based on the code below may get you started*" meaning you have to do some work. In the last part of my answer I wrote about initializing `array`. Your original question was quite precise in what you wanted, it did not ask for fully working code and you did not give enough details to do that. – AdrianHHH Jun 02 '13 at 20:25