RavynousHunter on 22/1/2009 at 05:34
Ok, this is starting to irritate me. I'm working on (
http://www.spoj.pl/problems/JAVAC/) this problem, and most of my program is (or at least seems to be) workable, except one bloody
for loop.
I keep getting this error:
Error 1 | Property or indexer 'string.this[int]' cannot be assigned to -- it is read only | Line 127 Column 17
The loop is as follows:
Code:
for ( int i = 0; i < score_locs.Length; i++ )
{
int new_loc = score_locs
- 1;
char new_char = Convert.ToChar( output[new_loc].ToString().ToUpper() );
output[new_loc] = new_char;
}
For those who want to know, "score_locs" is of type
int[], and "output" is of type
string.
The line in bold is the one that's trowing this error. Anyone got any clue what the hell VC# is talking about? :confused:
Striker on 22/1/2009 at 06:15
You can't directly assign values to a string using the [int] property, since it only has a Get, but not a Set, function.
Might be easier for you to create an ArrayList to store the new values and store this to output instead:
Code:
ArrayList outputArray = new ArrayList();
for ( int i = 0; i < score_locs.Length; i++ )
{
int new_loc = score_locs
- 1;
char new_char = Convert.ToChar( output[new_loc].ToString().ToUpper() );
outputArray.Add(new_char);
}
output = outputArray.ToString();
Firefreak on 22/1/2009 at 06:50
To add another opinion :)
When strings should be built dynamically, one should consider using the StringBuilder (from System.Text).
Apart from that, I found the referenced text interesting:
Quote:
Unlike them, C++ people use only small letters in their identifiers. (...)
Do they? While both .NET and Java have coding standards (including naming guidelines) defined by Microsoft and SUN respectively, C/C++ doesn't (have one unique and global). Especially naming guidelines are - if at all - defined by the corresponding context they are used in (project/company/...) - which may not match with the described form -- CamelCase is for example quite common as well...
Ulukai on 22/1/2009 at 09:15
I concur with Striker, an ArrayList would be a good choice here.
Also consider what Firefreak says about the StringBuilder.
From (
http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx) MSDN:
A String concatenation operation always allocates memory, whereas a StringBuilder concatenation operation only allocates memory if the StringBuilder object buffer is too small to accommodate the new data. Consequently, the String class is preferable for a concatenation operation if a fixed number of String objects are concatenated. It's definitely worth considering if you're doing many concatenations in a loop.
With regards to coding standards, in the environment I work in, we don't follow Microsoft's guidelines to the letter, but we have case onvention for parameters, member variables, locals etc. which everyone knows and uses.
The most important factor here being that you have
a convention of some kind, and everyone maintaining the code knows it and sticks to it.
RavynousHunter on 22/1/2009 at 09:28
Actually, I found a way around it; but another thing busted (doesn't it always?), but I know what's wrong there. ... Or at least I think I do.
Oddly enough, the ArrayList doesn't exist for me; what version of .Net are you referencing? I'm working with v3.5. That is if I haven't missed some library or somethin... I do that sometimes. :cheeky:
Ulukai on 22/1/2009 at 10:46
Just add in:
"using System.Collections" and it'll find it.
Incidently, you can just type "ArrayList", then press SHIFT+ALT+F10 and it will suggest and add in the missing assembly automatically if you subsequently press return.
Edit: Alternatively, you can press CTRL + Period/Full Stop
Al_B on 27/1/2009 at 19:00
I personally don't like ArrayLists as they're weakly typed - i.e. you can store anything in them. Sometimes you may need this on rare occasions but normally a collection should be of the same type. (Or if not the same type at least a list of a generic base type from which more specific types are derived). If you want to build a list of characters (or strings) just define it as:
Code:
List < Char> CharList = new List < Char>();
CharList.Add('a');
CharList.Add('b');
CharList.Add('c');
CharList.Add("something else") <- This will give an error
(Note: On the first line I've had to add an extra space between '<' and 'Char' as otherwise it will be removed by phpBB)
Even having said that, the code that's been suggested will not work. Calling "outputArray.ToString();" will more than likely simply return "System.Collections.ArrayList" which although interesting doesn't do what you want.
Ultimately, for the problem you're tackling you don't need either an ArrayList or a typed List. I knocked something up with doesn't use concatonation or lists. I'll post the code if you want (and for others to nitpick!) but working out how to do it yourself is obviously a better learning experience.
Ulukai on 27/1/2009 at 20:12
Don't get me started on the evil of .ToString() :)
dvrabel on 27/1/2009 at 20:54
Perhaps C++ programmers are smart enough to know that CamelCaseIsTruelyAwefulToRead, and Java programmers haven't yet realized that underscores_make_reading_identifiers_a_breeze?
Bjossi on 27/1/2009 at 21:12
As Firefreak mentioned above, giving a string object a new value inside a loop is a bad idea, better use StringBuilder in those cases. String objects are immutable and will therefore not change, resulting in new objects being created in every loop when a new value is assigned.