473,222 Members | 1,802 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,222 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 1198
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.