3

I am passing a message from a server that gets stored into a string variable called strObject. I wish to convert the string inside strObject to upper case. So, I use ToUpper() method. But, when I add a breakpoint and go through the line, my string is not getting converted into Upper case. strObject variable will always contain the text Task_status. I wish to convert it into TASK_STATUS. Am I missing anything? Posting my relevant code below:-

public void VerifyValue(String strObject, String strValue, int row)
        {               
            strObject.ToUpper().Trim();
            strValue.ToUpper().Trim();
            switch (strObject)
            {
                case "TASK_STATUS":
                    if (m_taskStatus.taskStatus.ToString() == strValue)
                    {
                        ExcelRecorder(null, row);
                    }
                    else
                    {
                        ExcelRecorder("The value [" + m_taskStatus.taskStatus.ToString() + "] does not match with the given value.", row);
                    }
            }
        }
user1501034
  • 343
  • 3
  • 6
  • 16

6 Answers6

7

strObject.ToUpper() returns a string in upper case

Use the following ...

strObject = strObject.ToUpper().Trim();
Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42
6

A System.String is immutable, so you must re-assign, like:

strObject = strObject.ToUpper().Trim(); 

All methods that manipulate strings, leave the original string unchanged and returns a new string with the desired content. You must pick up that return value and assign it to something.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
2

string.ToUpper() returns a value, use it.

bluish
  • 26,356
  • 27
  • 122
  • 180
burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
2

Strings are immutable, so calling the functions on the string doesn't actually change the strings. Instead they return a modified copy of the strings.

This means that you have to store the returned value of the functions, fore example in the original variable.

JonC
  • 978
  • 2
  • 7
  • 28
1

This won't work since strings are immutable, you have to assign the value back to strObject

strObject = strObject.ToUpper().Trim();

Also there is nothing much done by the switch as shown in your code, you can remove it unless this is not the entire code.

public void VerifyValue(String strObject, String strValue, int row)
{               
     //strObject.ToUpper().Trim();
     //strValue.ToUpper().Trim();
     if(strObject.ToUpper() ==  "TASK_STATUS")
     {
          if (m_taskStatus.taskStatus.ToString() == strValue.ToUpper())
          {
              ExcelRecorder(null, row);
          }
          else
          {
               ExcelRecorder("The value [" + m_taskStatus.taskStatus.ToString() + "] does not match with the given value.", row);
           }
       }
}
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
0

Strings are immutable, you need to remember that when you work with them.

Your method inserts values to your strObject and strValue variables to which you call various worker methods but those worker methods do not change the immutable string variables that have already been created.

Although the original variables are immutable you can still set new variables (Even with the same name) equal to the original plus the result of the worker method.

So all you really need to do is to change

strObject.ToUpper().Trim();

and

strValue.ToUpper().Trim();

to

strObject = strObject.ToUpper().Trim()

and

strValue = strvalue.ToUpper().Trim()

This answer is pretty much a duplicate of my answer here but I think both are correct.

Neil Meyer
  • 473
  • 4
  • 15