1

I need to assign my result to an array, but I'm facing a problem when I'm using this code:

string[] result = null;
result = new string[10];
int num = 0;
int id = Convert.ToInt32(textReader.GetAttribute("id"));
foreach (string attr in attrs)
  {
    // this line fails \/ 
    result[id] =  num { textReader.GetAttribute(attr) };
    // I can't put there correctly my int - num
    num++;
  }

Errors are: Cannot implicitly convert type 'int' to 'string' And two times: ; is expected

Can you tell me how to do it properly? I'm new to C# and I can't resolve this problem. Have a nice day.

edit

I want to create multi dimensional array so (php style) result[1] = array(0 => firstattr, 1 => secondattr and so on...)

3
  • What are trying to store in result[id]? Commented Mar 24, 2013 at 14:29
  • @mozart Nope, the problem is on line where I try to assign values to result array. Commented Mar 24, 2013 at 14:30
  • @Steve I try to store there values of my attributes of xml file - <mlvl id="1" ammount="206" nick="Reilla"/> - so "number" and "name". Commented Mar 24, 2013 at 14:32

3 Answers 3

1

Reading your update turns this into a different question. Unlike PHP, C# is serious about types, so you can't just stuff things into variables or array slots. You have to explicitly declare that you want a multi-dimensional array. In C# arrays are not as flexible as in PHP. In PHP, you can use anything as a key. In C#, you have to use a different type entirely (which is actually what PHP is doing behind the scenes as all arrays in PHP are hash tables internally).

I'm going to make another assumption, which is that part of this code is in a function that fills in attributes for a single id, and that your result variable is actually shared across multiple calls of this function. I can't see how your code would make sense otherwise.

// this is probably shared
Dictionary<int, List<string>> result = new Dictionary<int, List<string>>();

// perhaps this next bit is in a function that gets the attributes for a given tag
int id = Convert.ToInt32(textReader.GetAttribute("id"));
result[id] = new List<string>();
foreach(string attr in attrs) {
    result[id].Add(textReader.GetAttribute(attr));
}

What we have here is a hash of integers and growable arrays (C# calls these dictionaries). That is, the outer structure is equivalent to a PHP array that maps integers to subarrays. The subarrays are essentially the same, except we use List in C# because it can grow -- we don't have to allocate exactly the right number of elements when we create it. You could do that, but it might be extra work. I'll leave it as an exercise to the OP to implement it that way.

Just in case you aren't aware of what Dictionary<int, List<string>> means, you should read up on generics in C#. Translated to English, it basically says "dictionary (that is, hash table) with keys that are int and values that are List<string>, that is, list of values that are string". Note also that each list in the dictionary has to be initialized separately, which is why I have the line just above the foreach. PHP auto-reifies things, but C# does not.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your explanation. Maybe someday I'll be forced to use "List".
@Daniel: lists are super easy, so dive right in. They work like you'd want arrays to work (and if you're coming from PHP, how you probably already use them).
Okay, so I'll read about them as fast as I'll be able to do it. Thanks once again. And yes, I want C# arrays to work as PHP arrays because they're easier for me to understand.
0

How I created what I've wanted to do:

string[][] result = null;
int num = 0;
                        int id = Convert.ToInt32(textReader.GetAttribute("id"));
                        string[] arr = new string[3]; 
                        foreach (string attr in attrs)
                        {
                            arr[num] = textReader.GetAttribute(attr);
                            result[id] = arr;
                            num++;
                        }

2 Comments

Make sure to initialize result to the proper size. This code as written will fail at runtime.
This is only small part of my code. Now all works as I wanted.
0

textReader.GetAttribute(attr) will return a string, and this is being assigned to num which is an integer, which is why you get the error: "Cannot implicitly convert type 'int' to 'string'"

 // this line fails \/ 
 result[id] =  num { textReader.GetAttribute(attr) };
 // I can't put there correctly my int - num
 num++;

could be rewitten to:

string attribute = textReader.GetAttribute(attr);
result[id] =  attribute;
num = int.Parse(attribute) + 1;

1 Comment

I think you mean int.Parse(attribute) + 1. You can't increment an intermediate value with ++.

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.