Al_B on 27/1/2009 at 23:56
Agreed - but I'm struggling to see where someone is suggesting that a string should be built up in that way. The first post attempts to assign a character to a position in a string - but in a way which gives a compile error.
Bjossi on 28/1/2009 at 00:17
Yeah, afaik 'output[new_loc]' is not going to work when output is of type 'string'.
Ulukai on 28/1/2009 at 14:02
Is this what you were trying to do? Note the algortihm does not work in terms of the problem you're trying to solve, but converting to the char array lets you change a specific characater in
output.
Code:
private string ConvertString(string input, int[] score_locs)
{
char[] output = input.ToCharArray();
StringBuilder SB = new StringBuilder();
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 (int j = 0; j < output.Length; j++)
{
SB.Append(output[j]);
}
return SB.ToString();
}
Al_B on 28/1/2009 at 23:38
I agree with much of that - but you probably don't need the StringBuilder. Why not just create a string directly from the modified array?
Code:
private string ConvertString(string input, int[] score_locs)
{
char[] output = input.ToCharArray();
for (int i = 0; i < score_locs.Length; i++)
{
output[score_locs
- 1] = char.ToUpper(output[score_locs - 1;]);
}
return new string(output);
}Easier to understand - and probably more efficient.
RavynousHunter on 29/1/2009 at 01:19
Actually, I fixed it with this:
Code:
string outputArray = "";
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 += new_char;
}
output = outputArray;
That part compiles like a champ, I just happen to have a problem with another part, but like I said before, I think I know what needs to be done there...
I always heard from those well-versed in computers (before I became one of them) that "ya fix one thing, and a dozen others break." Never thought it was true... Until I started programming on my TI-83. Even on something
that basic (no pun intended), the number of problems I got from what appeared to me (at the time) to be perfectly normal, compilable code, threw a slew of errors.
Truly, those who survive to call themselves professionals must have a heroic amount of patience.
Ulukai on 29/1/2009 at 08:47
Quote Posted by Al_B
I agree with much of that - but you probably don't need the StringBuilder. Why not just create a string directly from the modified array?
Only working with char arrays once in a blue moon, couldn't quite remember how to do it off the top of my head :)
RavynousHunter on 30/1/2009 at 01:29
Quote Posted by Ulukai
Only working with char arrays once in a blue moon, couldn't quite remember how to do it off the top of my head :)
You and me both, brother. One of the first things I learned when I started using C++ was to make friends with the std::string class. Since then, I've learned to use "string"s whenever I can, because char arrays can be confusing as hell.
As one of the more popular people on GameDev.net said:
Quote Posted by unknown
If your code has 'char' in it, you have a bug.
[edit]
Quote Posted by Al_B
I personally don't like ArrayLists as they're weakly typed - i.e. you can store anything in them.
I can see how this would be useful in game design for things such as inventory, as you could have an ArrayList store both a list of "item" objects and their quantities. ... Then again, you could always store the quantity in a variable inside the class itself. ...
[/edit]
Al_B on 31/1/2009 at 16:01
Quote Posted by RavynousHunter
As one of the more popular people on GameDev.net said:
Quote:
Originally Posted by unknownIf your code has 'char' in it, you have a bug.
That's a bit of a sweeping statement - but I can see where he's coming from. Removing 'char' doesn't make your code inherently less buggy.
Quote Posted by RavynousHunter
I can see how this would be useful in game design for things such as inventory, as you could have an ArrayList store both a list of "item" objects and their quantities. ... Then again, you could always store the quantity in a variable inside the class itself. ...
Yes, exactly. Mixing quantities and item objects would be very difficult to maintain - much better to create a list of InventoryItem objects. You can then obviously create derived WeaponItem or FoodItem classes if you need to mix different 'types' of inventory items in the list.
RavynousHunter on 31/1/2009 at 21:40
Of course, you could make the array two dimensional, like so:
Code:
Inventory[items][quantity] = new array[MAX_ITEMS][1]
That would clean things up somewhat, so long as you didn't accidentally write somewhere to add an Item object to the Quantity field and vice versa. However, a "quantity" variable is still much, much simpler.
[edit][start_java_rant]
Quote Posted by dvrabel
Perhaps C++ programmers are smart enough to know that CamelCaseIsTruelyAwefulToRead, and Java programmers haven't yet realized that underscores_make_reading_identifiers_a_breeze?
That's probably because most Java programmers are kids who just want to work with something "cool" and "modern" instead of old, dry, and "hard" C++. (personally, after I learned some simple syntax and such, C++ was a breeze mostly) While, on the other hand, C++ programmers tend to be older, or at least more experienced, and therefore like their stuff to be easier to read, usually because there is a LOT more of it to read.
[/edit][end_java_rant]
Al_B on 1/2/2009 at 00:46
Quote Posted by RavynousHunter
Of course, you could make the array two dimensional, like so:
Code:
Inventory[items][quantity] = new array[MAX_ITEMS][1]
That would clean things up somewhat, so long as you didn't accidentally write somewhere to add an Item object to the Quantity field and vice versa. However, a "quantity" variable is still much, much simpler.
Yes - it is. The above array won't work as shown as you're mixing one type of object with an integer value. A 2 dimensional array of generic objects would work, as would two arrays but both are very bad ideas and difficult to maintain.
I suspect from what you've posted that you're still learning / finding out programming techniques. If so, read up basic OO design. It doesn't matter about the language - you can be object oriented in assembler if necessary!
Quote Posted by RavynousHunter
[start_java_rant]
That's probably because most Java programmers are kids who just want to work with something "cool" and "modern" instead of old, dry, and "hard" C++. (personally, after I learned some simple syntax and such, C++ was a breeze mostly) While, on the other hand, C++ programmers tend to be older, or at least more experienced, and therefore like their stuff to be easier to read, usually because there is a LOT more of it to read.
[/edit][end_java_rant]
Experienced C++ programmers are probably older because it's a far older language than java. Good design is far more important than language choice and the readibilty of functions, variables etc. has very little to do with whether they contain underscores or capital letters.