String read only error

  • Thread starter Thread starter rithesh.rg
  • Start date Start date
R

rithesh.rg

Hello,

Given below is a part of my code. In the code i am trying to parse
through a string and replace all "+" characters by "*".


string enc_msg = Convert.ToBase64String(full_enc);

for (i = 0; i < enc_msg.Length; i++)
{
if (enc_msg == '+' )
{
enc_msg = '*';
}

}


However when i build, i get the following error -
" Property or indexer 'string.this[int]' cannot be assigned to -- it
is read only "

Could someone kindly let me know whats wrong with my code and suggest
any changes

Regards,
Rithesh Swamy
 
(e-mail address removed) wrote:

Hi,
However when i build, i get the following error -
" Property or indexer 'string.this[int]' cannot be assigned to -- it
is read only "

Could someone kindly let me know whats wrong with my code and suggest
any changes

Strings cannot be changed. You'll have to create a new one every time
you want to make changes. Also there already is a String.Replace
function, you shouldn't implement your own like you did with the for loop:

enc_msg = enc_msg.Replace('+', '*');

hth,
Max
 
hi,
the string object is immutable. You cannot call it as an array and
change it. Try using the .NET 'String.Join' object instead ( note the
capital S) . Or simply create a new string object.

James Jenkins
http://www.tamarsolutions.co.uk
 
Given below is a part of my code. In the code i am trying to parse
through a string and replace all "+" characters by "*".

string enc_msg = Convert.ToBase64String(full_enc);

for (i = 0; i < enc_msg.Length; i++)
{
if (enc_msg == '+' )
{
enc_msg = '*';
}

}


However when i build, i get the following error -
" Property or indexer 'string.this[int]' cannot be assigned to -- it
is read only "

Could someone kindly let me know whats wrong with my code and suggest
any changes


Strings in .NET are an immutable type - once you've constructed an
instance, you can't ever change it. Use a StringBuilder instead, if you
want to index the string and assign directly - but you'll still end up
with a copy.

By the way, one way to do what you're trying to do is this:

enc_msg = enc_msg.Replace('+', '*');

-- Barry
 
strings are immutable, thus the indexer of the string object is defined as
public char this[int index] { get; }

Change the enc_msg from string to StringBuilder and you are ok.

StringBuilder enc_msg = new StringBuilder(Convert.ToBase64String(full_enc));

OR

You could use string.Replace to change all '+' to '*'. In this way you do
not need the loop.

string enc_msg = Convert.ToBase64String(full_enc).Replace('+','*');

Of course this creates temporary objects so it increases memory usage a
bit..
 
Thanks a lot guys, both the Replace and StringBuilder solved my
problem.

Regrads,
Rithesh Swamy



Laura said:
strings are immutable, thus the indexer of the string object is defined as
public char this[int index] { get; }

Change the enc_msg from string to StringBuilder and you are ok.

StringBuilder enc_msg = new StringBuilder(Convert.ToBase64String(full_enc));

OR

You could use string.Replace to change all '+' to '*'. In this way you do
not need the loop.

string enc_msg = Convert.ToBase64String(full_enc).Replace('+','*');

Of course this creates temporary objects so it increases memory usage a
bit..

Hello,

Given below is a part of my code. In the code i am trying to parse
through a string and replace all "+" characters by "*".


string enc_msg = Convert.ToBase64String(full_enc);

for (i = 0; i < enc_msg.Length; i++)
{
if (enc_msg == '+' )
{
enc_msg = '*';
}

}


However when i build, i get the following error -
" Property or indexer 'string.this[int]' cannot be assigned to -- it
is read only "

Could someone kindly let me know whats wrong with my code and suggest
any changes

Regards,
Rithesh Swamy
 
Like Java, string type is Immutable in .NET. So you cannot change it. try
stringbuilder.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top