-
December 2nd, 2018, 12:10 PM
#1
Converting C++ to C# that cannot be assigned
Hey all I am traznslating some C++ code over to C# and I am at a stopping place due to an error that I cant seem to figure out:
My c# code:
Code:
public static String inputStr;
public static int inputLength;
public static void inputCheck()
{
inputLength = inputStr.Length;
int i = 0;
while (i < inputLength) {
if (inputStr[i] > 95 && inputStr[i] < 123) {
inputStr[i] -= 32;
}
i++;
}
}
The error is on this line:
inputStr[i] -= 32;
And its saying:
Error CS0200 Property or indexer 'string.this[int]' cannot be assigned to -- it is read only
Any help would be great as I'm not sure why I am even getting an error like that.
-
December 2nd, 2018, 04:40 PM
#2
Re: Converting C++ to C# that cannot be assigned
C# strings are immutable, so string operations that manipulate a string always create a new string. The error you are getting is because you are trying to change a character in the string which isn't allowed. You can change the string by copying to a new string or by using the StringBuilder class. Btw, why are you using static here?
-
December 3rd, 2018, 02:27 AM
#3
Re: Converting C++ to C# that cannot be assigned
Originally Posted by Arjay
C# strings are immutable, so string operations that manipulate a string always create a new string. The error you are getting is because you are trying to change a character in the string which isn't allowed. You can change the string by copying to a new string or by using the StringBuilder class. Btw, why are you using static here?
Can you provide an example?
-
December 3rd, 2018, 10:40 AM
#4
Re: Converting C++ to C# that cannot be assigned
Originally Posted by StealthRT
Can you provide an example?
You could loot at the examples with Insert and Remove here: https://docs.microsoft.com/en-us/dot.../stringbuilder
Victor Nijegorodov
-
December 3rd, 2018, 05:47 PM
#5
Re: Converting C++ to C# that cannot be assigned
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|