c# - Exception in array assignment : Cannot implicitly convert type 'int' to 'string' And two times: -
i need assign result array, i'm facing problem when i'm using code:
string[] result = null; result = new string[10]; int num = 0; int id = convert.toint32(textreader.getattribute("id")); foreach (string attr in attrs) { // line fails \/ result[id] = num { textreader.getattribute(attr) }; // can't put there correctly int - num num++; }
errors are: cannot implicitly convert type 'int' 'string' , 2 times: ; expected
can tell me how properly? i'm new c# , can't resolve problem. have nice day.
edit
i want create multi dimensional array (php style) result[1] = array(0 => firstattr, 1 => secondattr , on...)
reading update turns different question. unlike php, c# serious types, can't stuff things variables or array slots. have explicitly declare want multi-dimensional array. in c# arrays not flexible in php. in php, can use key. in c#, have use different type entirely (which php doing behind scenes arrays in php hash tables internally).
i'm going make assumption, part of code in function fills in attributes single id
, , result
variable shared across multiple calls of function. can't see how code make sense otherwise.
// shared dictionary<int, list<string>> result = new dictionary<int, list<string>>(); // perhaps next bit in function gets attributes 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 have here hash of integers , growable arrays (c# calls these dictionaries). is, outer structure equivalent php array maps integers subarrays. subarrays same, except use list
in c# because can grow -- don't have allocate right number of elements when create it. that, might work. i'll leave exercise op implement way.
just in case aren't aware of dictionary<int, list<string>>
means, should read on generics in c#. translated english, says "dictionary (that is, hash table) keys int
, values list<string>
, is, list of values string
". note each list in dictionary has initialized separately, why have line above foreach. php auto-reifies things, c# not.
Comments
Post a Comment