0

I am storing some boolean values as string session variables, like this:

HttpContext.Session.SetString("SomeBool", user.SomeBool.ToString().ToLower());

This gives me warnings that the result could vary depending on the users culture. How is that possible? In what way could the result of ToString() or ToLower() of "True" or "False" vary depending on culture? Aren't boolean values always represented by the English words "True" and "False", no matter the culture of the database or hosting environment?

I've also tried these three, which all gives exactly the same warning:

HttpContext.Session.SetString("SomeBool", FormattableString.Invariant($"{user.SomeBool.ToString().ToLower()}"));

HttpContext.Session.SetString("SomeBool", String.Format(CultureInfo.CurrentCulture, $"{0}", user.SomeBool.ToString().ToLower()));

HttpContext.Session.SetString("SomeBool", String.Format(CultureInfo.CurrentCulture, user.SomeBool.ToString().ToLower()));

VS suggests I can disable CA1305 warnings, but I don't want to do that.

Any suggestions?

Update

Although VillageTech's answer answers my question, I have changed my code to avoid the issue altogether. Inspired by Christopher's suggestion about hard coding the values:

HttpContext.Session.SetString("SomeBool", user.SomeBool ? "true" : "false");
Stian
  • 1,522
  • 2
  • 22
  • 52

5 Answers5

4

Do not store stuff as string. Arguably string is the 2nd worst type for processing. The only one worse is raw binary.

In the rare case that you have to store something as String, you must make certain to pick the same Culture Settings and Encoding at all endpoints. By default the ToString() and Parse() and generally string related functions extract the current Culture and it's settings from Windows. This is very usefull for the normal GUI, but very bad for such a case. Those kinds of issues is what the warning is about. Hardcode one instead.

XML and JSON do take care of those things for you. But in this case you have to do it manually.

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • You mean like this: `HttpContext.Session.SetString("SomeBool", user.SomeBool ? "true" : "false");` ? – Stian Dec 07 '19 at 22:17
  • @Stian I can not find a definition for `SetString()` anywhee in what this should be: https://learn.microsoft.com/en-us/dotnet/api/system.web.sessionstate.httpsessionstate – Christopher Dec 07 '19 at 23:27
  • I'm using .Net Core. `HttpContext.Session` has three methods for setting values - `Set()` (which I don't use), `SetString()` and `SetInt32()`. – Stian Dec 08 '19 at 18:46
1

Both ToString() and ToLower() can emit such warnings. Use this:

HttpContext.Session.SetString("SomeBool", user.SomeBool.ToString(CultureInfo.CurrentCulture).ToLower(CultureInfo.CurrentCulture));
VillageTech
  • 1,968
  • 8
  • 18
  • 1
    Ah, ok. That works. But ... Wow! That's incredibly bulky syntax! Not very readable at all! – Stian Dec 07 '19 at 22:00
  • These are standard overloads for ToString() and ToLower(). For example: https://learn.microsoft.com/dotnet/api/system.string.tolower?view=netframework-4.8#System_String_ToLower_System_Globalization_CultureInfo_ – VillageTech Dec 07 '19 at 22:02
  • 1
    Your answer answers my question, but inspired by Christopher's answer, I have changed my code to this: `HttpContext.Session.SetString("SomeBool", user.SomeBool ? "true" : "false");`, avoiding the issue alltogether. – Stian Dec 08 '19 at 18:51
0

Try this


 // calling getValue() method 
stringSomeBool = getValue(user.SomeBool); 

HttpContext.Session.SetString("SomeBool", stringSomeBool.ToLower()); 

    // defining getValue() method 
    public static void getValue(bool variable) 
    { 

        // getting the value of string property 
        string value = variable.ToString(); 

        // print the string property 
        Console.WriteLine("{0}", value); 
    } 
unkn0wn95
  • 36
  • 4
0

my first contribution: Try defining it as a string before you use the variable like so:

string convd = user.SomeBool.ToString();
convd = convd.ToLower();
HttpContext.Session.SetString("SomeBool", convd);

This is what i normally would do, hope this helps as my first answer :))

Draugr
  • 1
  • 1
  • 2
0

Let FormattableString.Invariant do the ToString() for you:

using static System.FormattableString; 

HttpContext.Session.SetString("SomeBool", Invariant($"{user.SomeBool}").ToLower();
jaspenlind
  • 349
  • 2
  • 6