Why does Resharper give me a 'Localizable Interpolated String' hint here? - c#

I'm using VS 2015 with Resharper Ultimate for a Win Forms application and was doing something as simple as setting the text property of a button when I saw the blue squiggly line below the string.
Clicking on it converts the statement to the one I've retyped just below the problematic statement and there's no blue squiggly. Note that it's got the verbatim and the interpolation operators.
Also, setting the same string to another string with just interpolation seems to work fine. There's no characters to escape in this string. What's happening here?

As for verbatim string: Untick "ReSharper | Options | Code Editing | C# | Localization | Don't analyse verbatim string" checkbox and then ReSharper will show you such kind of suggestion for verbatim string as well.
As for why it shows squiggle for WinForms property but does not for a string: please refer to a webhelp article about "Localizable inspector" property. In case you would like to get the suggestion for all strings as well, you need to change "Localizable inspector" value to "Pessimistic"

You are setting a UI element's text to a string, and UI should be subject to localization.
On the other hand, interpolation is equivalent to just calling string.Format with default format provider.
It is assumed that UI elements will be populated from resources and that they will be formatted using specific culture. That is why ReSharper is sensitive to this particular use of string interpolation.
What you said about setting a different string to the interpolated string and then assigning that to the UI element, you are just playing tricks on ReSharper. It was not able to infer that you have assigned the UI element to the string which is not localized, but the problem is still there.

Related

Asp.net mvc display name attribute with #

I used resharper for my asp.net mvc application to format code and it changed all my code with # in DisplayName attribute. I tried searching for this in google to find out what # does here but I cant find satisfactory search result. COuld some on tell me about the difference between with and with out # in displayname attribute.
[DisplayName("Meeting Date")]
[DisplayName(#"Meeting Date")]
The fact that this is being done in an attribute is immaterial. Using the '#' character in front of a string literal is called a verbatim string and causes any escape sequence to be ignored, allowing you to do this:
var filename = #"C:\Filename.ext"
instead of this:
var filename = "C:\\Filename.ext"
Lots of people like that because it is prettier. Resharper likes to put that '#' symbol in front of string literals.
As I recall Resharper suggests you either move localizable strings into a resource file or make them verbatim. In your case, it looks like you accepted the suggestion (either explicitly or via code cleanup) to make a string verbatim. You can turn off the verbatim string suggestion under Resharper --> Options --> Code Editing --> C# --> Context Actions --> Convert to verbatim string.
Actually, I'm NOT sure why ReSharper would have detected an Attribute constructor value as localizable since they must be compile-time constant, so it probably did that based on some other condition. A quick email to support#jetbrains.com should get that sorted out for you pretty quickly.
NOTE: The '#' symbol can also be used in front of a reserved word in order to use that as a variable name, though this is not the case in your example above:
var #string = "string";

Which passwordchar shows a black dot (•) in a winforms textbox using code? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Which passwordchar shows a black dot (•) in a winforms textbox?
Unicode encoding for string literals in C++11
I want to use code to reveal the password or make it a dot like •
textBoxNewPassword.PasswordChar = (char)0149;
How can I achieve this?
http://blog.billsdon.com/2011/04/dot-password-character-c/ suggests '\u25CF';
Or try copy pasting this •
(not exactly an answer to your question, but still)
You can also use the UseSystemPasswordChar property to select the default password character of the system:
textBoxNewPassword.UseSystemPasswordChar = true;
Often mapped to the dot, and always creating a consistent user experience.
You need to look into using the PasswordBox control and setting the PasswordChar as *.
Example:
textBox1.PasswordChar = '*'; // Set a text box for password input
Wikipedia has a table of similar symbols.
In C#, to make a char literal corresponding to U+2022 (for example) use '\u2022'. (It's also fine to cast an integer literal as you do in your question, (char)8226)
Late addition. The reason why your original approach was unsuccessful, is that the value 149 you had is not a Unicode code point. Instead it comes from Windows-1252, and Windows-1252 is not a subset of Unicode. In Unicode, decimal 149 means the C1 control code "Message Waiting".
You could translate from Windows-1252 with:
textBoxNewPassword.PasswordChar =
Encoding.GetEncoding("Windows-1252").GetString(new byte[] { 149, })[0];
but it is easier to use the Unicode value directly of course.
In newer versions of .NET, you need to call:
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
before you can use something like Encoding.GetEncoding("Windows-1252").
textBoxNewPassword.PasswordChar = '\u25CF';

formatting strings with backslash

I'm a newbie to c# so hopefully this one isn't too hard for a few of you.
I'm trying to build a string that has a \ in it and I am having difficulty getting just one backslash to show up even though I am adding additional escape chars or ignoring them all together. Can someone show me what I am doing wrong?
What I want my string to look like:
"10.20.14.103\sql08"
What I've tried so far:
I added an additional character to make the compiler happy but it did not escape it.
ip = string.Format("{0}\\\\{1}", ip, instancename); // output has 2 \'s
I told it to ignore escapes, it decided to ignore me instead
string temp = #"192.168.1.200\sql08"; // output has 2 \'s
Can someone help me make sense of this? (The richtext editor here seems to do a better job with it than VS2010 is doing, lol)
I'm guessing you're getting confused by the debugger.
If you hover your mouse over a local variable in VS, strings will be escaped so a single \ will display as \\.
To see what your string really is, output it somewhere for display (e.g., to the console) or hover your mouse on the variable, click on the arrow next to the little magnifying glass that appears, and select "Text Visualizer."
If you're looking at these strings in the debugger (i.e., by hovering the mouse over the variable or using a watch), the debugger adds escape characters to the display string so that it's a valid string expression. If you want to view the string verbatim in this fashion, click on the magnifying glass on the right side of the tooltip or watch entry with the string in it.
I'm guessing you're looking at the values in the debugger and seeing that they have two slashes.
That's normal. The debugger will show two slashes even though the actual string representation will only have one. Just another hump to get over when getting used to the debugger.
Be assured that when you actually use your strings, they will still only have a single slash (using either of your methods).
string requiredString = string.Format(#"{0}\\{1}",str1,str2);

Verbatim strings and Visual Studio

Is there a way to get Visual Studio to display strings as verbatim strings (prefixed with '#')? I'd like to easily cut strings containing file paths from Visual Studio into Explorer or other apps.
Clarification: when VS displays a string in the auto, watch, immediate, etc. window, I'd like it to be formatted as as verbatim string so that I can simple copy it for use elsewhere.
You can click on the magnifier in VS 2008 debugger variable display and select "Text Visualizer" which will give you the text in an unmassaged format.
You may need to clarify your question. My first thought was that you just need to prefix the string with '#' to make them verbatim, but you already know that.
string s = #"c:\my folder\";
What exactly are you trying to do with the string?
Here's a discussion on how to actually build verbatim-ized strings:
Can I convert a C# string value to a string literal.
Plugging this in a debug vizualiser is a different story...

C#: How do you go upon constructing a multi-lined string during design time?

How would I accomplish displaying a line as the one below in a console window by writing it into a variable during design time then just calling Console.WriteLine(sDescription) to display it?
Options:
-t Description of -t argument.
-b Description of -b argument.
If I understand your question right, what you need is the # sign in front of your string. This will make the compiler take in your string literally (including newlines etc)
In your case I would write the following:
String sDescription =
#"Options:
-t Description of -t argument.";
So far for your question (I hope), but I would suggest to just use several WriteLines.
The performance loss is next to nothing and it just is more adaptable.
You could work with a format string so you would go for this:
string formatString = "{0:10} {1}";
Console.WriteLine("Options:");
Console.WriteLine(formatString, "-t", "Description of -t argument.");
Console.WriteLine(formatString, "-b", "Description of -b argument.");
the formatstring makes sure your lines are formatted nicely without putting spaces manually and makes sure that if you ever want to make the format different you just need to do it in one place.
Console.Write("Options:\n\tSomething\t\tElse");
produces
Options:
Something Else
\n for next line, \t for tab, for more professional layouts try the field-width setting with format specifiers.
http://msdn.microsoft.com/en-us/library/txafckwd.aspx
If this is a /? screen, I tend to throw the text into a .txt file that I embed via a resx file. Then I just edit the txt file. This then gets exposed as a string property on the generated resx class.
If needed, I embed standard string.Format symbols into my txt for replacement.
Personally I'd normally just write three Console.WriteLine calls. I know that gives extra fluff, but it lines the text up appropriately and it guarantees that it'll use the right line terminator for whatever platform I'm running on. An alternative would be to use a verbatim string literal, but that will "fix" the line terminator at compile-time.
I know C# is mostly used on windows machines, but please, please, please try to write your code as platform neutral. Not all platforms have the same end of line character. To properly retrieve the end of line character for the currently executing platform you should use:
System.Environment.NewLine
Maybe I'm just anal because I am a former java programmer who ran apps on many platforms, but you never know what the platform of the future is.
The "best" answer depends on where the information you're displaying comes from.
If you want to hard code it, using an "#" string is very effective, though you'll find that getting it to display right plays merry hell with your code formatting.
For a more substantial piece of text (more than a couple of lines), embedding a text resources is good.
But, if you need to construct the string on the fly, say by looping over the commandline parameters supported by your application, then you should investigate both StringBuilder and Format Strings.
StringBuilder has methods like AppendFormat() that accept format strings, making it easy to build up lines of format.
Format Strings make it easy to combine multiple items together. Note that Format strings may be used to format things to a specific width.
To quote the MSDN page linked above:
Format Item Syntax
Each format item takes the following
form and consists of the following
components:
{index[,alignment][:formatString]}
The matching braces ("{" and "}") are
required.
Index Component
The mandatory index component, also
called a parameter specifier, is a
number starting from 0 that identifies
a corresponding item in the list of
objects ...
Alignment Component
The optional alignment component is a
signed integer indicating the
preferred formatted field width. If
the value of alignment is less than
the length of the formatted string,
alignment is ignored and the length of
the formatted string is used as the
field width. The formatted data in
the field is right-aligned if
alignment is positive and left-aligned
if alignment is negative. If padding
is necessary, white space is used. The
comma is required if alignment is
specified.
Format String Component
The optional formatString component is
a format string that is appropriate
for the type of object being formatted
...

Categories