Problem setting string class to read-write

  • Thread starter Thread starter travis powell
  • Start date Start date
T

travis powell

i have created a string class. later on i created a procedure where i
need to change charaters in the string. it seems to work just fine unitl
i want to assigm the new character values to a particular position in
the string [see starred lines at the bottom]. i get the error message
that the Property or Indexer is Read-only on compile. can anyone see
where i am going wrong? thanks in advance.


public class InputString
{
private string strParseString;

// A read-write instance property:
public string InStr
{
get
{
return strParseString;
}
set
{
strParseString = value;
}
}
}

public void CharaChange(string ssInStr, int nnInPos, int nnCnt)
{
char strTmp1;
char strTmp2;

InputString strCharaChange = new InputString();
strCharaChange.InStr = ssInStr;

strTmp1 = strCharaChange.InStr[nnInPos];
strTmp2 = strCharaChange.InStr[nnCnt];

strCharaChange.InStr[nnInPos] = strTmp2; //*******************
strCharaChange.InStr[nnCnt] = strTmp1; //*******************

}
 
..NET strings are immutable.
You cannot change them, you can only create a new string and reassign it.

If you need to pass a "modifiable" string, you should pass a
System.Text.StringBuilder.
 
Back
Top