473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

writing to a strings index

I have this code:
string test = new string(' ', 2);
test[0] = 't';

and I'm getting a compiler error that string.this[int] is read only. Hmmm.
Why? I didn't declare it const or readonly?
Dec 6 '05 #1
6 1204
Strings are immutable. They cannot be changed.

"Steve" <ss*@sss.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
I have this code:
string test = new string(' ', 2);
test[0] = 't';

and I'm getting a compiler error that string.this[int] is read only.
Hmmm.
Why? I didn't declare it const or readonly?

Dec 6 '05 #2
strings in .Net are immutable - i.e. you cannot change their content once
created; depending on your intended usage, you could use substring /
replace/ regex functions to create a new string based on yours, or you could
call .ToCharArray(), change the array, and then create a new string from the
array (one of the ctors accepts char[]).

Marc

"Steve" <ss*@sss.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
I have this code:
string test = new string(' ', 2);
test[0] = 't';

and I'm getting a compiler error that string.this[int] is read only.
Hmmm.
Why? I didn't declare it const or readonly?

Dec 6 '05 #3
Steve,

Strings are treated as immutable. Once you create one, you can't change
the contents of it. You can use it as an input to create new strings.

In this case, you would have to create a new string, and concatenate
them together, like so:

test = (new String('t', 1)) + test.Substring(1);

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve" <ss*@sss.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
I have this code:
string test = new string(' ', 2);
test[0] = 't';

and I'm getting a compiler error that string.this[int] is read only.
Hmmm.
Why? I didn't declare it const or readonly?

Dec 6 '05 #4
KH
Try the StringBuilder class
"Marc Gravell" wrote:
strings in .Net are immutable - i.e. you cannot change their content once
created; depending on your intended usage, you could use substring /
replace/ regex functions to create a new string based on yours, or you could
call .ToCharArray(), change the array, and then create a new string from the
array (one of the ctors accepts char[]).

Marc

"Steve" <ss*@sss.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
I have this code:
string test = new string(' ', 2);
test[0] = 't';

and I'm getting a compiler error that string.this[int] is read only.
Hmmm.
Why? I didn't declare it const or readonly?


Dec 6 '05 #5
great, thanks for all the answers everyone! While I don't understant why
they implemented the string class like this, I'm sure there is a logical and
sound explanation. I have since used substring to accomplish what I need.

Thanks again!

"Steve" <ss*@sss.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
I have this code:
string test = new string(' ', 2);
test[0] = 't';

and I'm getting a compiler error that string.this[int] is read only. Hmmm. Why? I didn't declare it const or readonly?

Dec 6 '05 #6
Steve,

Mainly for performance reasons, as well as confusion... It just makes a
good number of things easier knowing it won't change.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve" <ss*@sss.com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
great, thanks for all the answers everyone! While I don't understant why
they implemented the string class like this, I'm sure there is a logical
and
sound explanation. I have since used substring to accomplish what I need.

Thanks again!

"Steve" <ss*@sss.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
I have this code:
string test = new string(' ', 2);
test[0] = 't';

and I'm getting a compiler error that string.this[int] is read only.

Hmmm.
Why? I didn't declare it const or readonly?


Dec 6 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Michael B. Allen | last post by:
Coming from C and Java on *nix I'm a little out of my element messing around with CList and MSVC++ but I think my issues are largely syntactic. I have an ADT that I use called a 'varray' that can...
5
by: Gary McCullough | last post by:
What I want to do sounds simple, but it's defeating me. I want to substitute all occurences of a colon : character in a string with an @ character -- unless the : occurs within a single or...
3
by: Daniel Weinand | last post by:
hello ng, i have a problem and a imho an insufficient method of solution. strings should be sorted by specific text pattern and dispayed in groups. the strings are stored in a db and have the...
2
by: gonzo | last post by:
So I have a project where I'm supposed to have a .txt input file of no more than ten first names, last names and birth years, and than in a menu I'm to give the user some options as to how the...
9
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No,...
9
by: Omatase | last post by:
I have a set of about 6 or so strings that I need to use to generate a unique hash. This hash will become the unique key in a database so the hash has to be the same each time I gen it for any 1...
28
by: hlubenow | last post by:
Hello, I really like Perl and Python for their flexible lists like @a (Perl) and a (Python), where you can easily store lots of strings or even a whole text-file. Now I'm not a...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
17
by: john | last post by:
All: I'm a long-time developer, new to PHP.... Is there an idiom used in PHP to construct SQL statments from $_POST data? I would guess that in many applications, the data read from $_POST...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.