dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Converting C++ to C# that cannot be assigned

  1. #1
    Join Date
    Aug 2008
    Posts
    99

    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.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,836

    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?

  3. #3
    Join Date
    Aug 2008
    Posts
    99

    Re: Converting C++ to C# that cannot be assigned

    Quote Originally Posted by Arjay View Post
    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?

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,262

    Re: Converting C++ to C# that cannot be assigned

    Quote Originally Posted by StealthRT View Post
    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

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,836

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)