Question Cannot assign value to local variable...


New member
Sep 26, 2014
Programming Experience
Hi Everyone!

I'm learning C# and just practicing a bit of coding and getting a bit stuck..

So I've got this function (I've cut out the unnecessary bits..):
public static string solved(string puzzle) { 
    firstChar = 'a';
    for (int i = 0; i < 10; i++) {
        string newPuz = string.Copy(puzzle);        
        for (int j = 0; j < newPuz.Length; j++) {
            if (newPuz[j] == firstChar) {
                newPuz[j] = (char)i;
        string newAnz = solved(newPuz);

When I try to compile it I get an error saying "Property or indexer 'string.this[int]' cannot be assigned to -- it is read only.

Basically I need to create a new string which is a copy of the original then change some of the characters (to a number between 0 and 9) and it's a recursive function.

I can't figure out how to get rid of the error :(

Thanks in advance!
Hi guys,

Don't worry I fixed it. used the string.ToCharArray() function to first convert it to a character array.

I've got another question now... same bit of code. In the line
newPuz[j] = (char)i;

i = 0, and I want '0' to be put in newPuz[j]. But it doesn't seem to happen... instead newPuz[j] becomes '\0'..

The number 0 is interpreted as the string terminator; that is the same as '\0'.
If you want the text representation of a number, use the toString() method.

newPuz[j] = i.toString();
Top Bottom