Is there any way to get generation of switch statements by enum?
For example if I have big enum and I have method with has parameter enum, then I need to code switch statements for each case, but is there a way to generate this switch statement by enum?
If I have a lot of other enums if my enums wide range and if I have 10-100 methods with different enums parameters in that case switch coding became a hell.
Probably it is good idea to create t4 template, anyway I am happy to hear any suggestions about enum switch generation.
Looking for solution without any additional software like Resharper.
Visual Studio already does this.
Type switch, tab to complete that snippet, type the enum name and you'll get the case statement autogenerated.
Visual Studio does this with a snippet.
Type switch, press Tab and then enter the name of your enum variable, and press Enter.
You should get a switch statement with each enum member case'd.
Tried messing with both of the above answers and couldn't get it to work for me. The big difference I found is that you have to double tap Tab after you start typing switch.
A single tab will complete the switch statement but won't open up parentheses. By double tapping it will open up paratheses and leave your cursor in the middle, so when you start typing the enum variable and tab to autocomplete it, you will generate all of the switch statements.
Related
This mostly comes up while trying to debug F# code in the Visual Studio debugger, which expects C# syntax:
The debugger does not recognize F# expressions. To enter an expression in a debugger window or a dialog box during F# debugging, you must translate the expression into C# syntax. When you translate an F# expression into C#, make sure to remember that C# uses == as the comparison operator for equality and that F# uses a single =.
(from http://msdn.microsoft.com/en-us/library/vstudio/ee843932.aspx)
The problem is that I have variables and functions that contain single quote characters, like path', but single quotes seem not to be valid in names in C#, so entering path' in the debugger window just says Newline expected in constant or words to that effect, because it thinks I am starting a character literal. Even right-clicking on it and adding a watch via the contextual menu gives a similar error.
How can I translate F# names containing single quotes into C# syntax, so that I can inspect them from the debugger or refer to them from C# code?
I do not think there is a way to do this - so the best option is probably to rename the variable path' to some other name that is a valid C# identifier like pathAux.
Although F# is more flexible with naming (and allows things like path' or even ``whatever !``), it is probably a good idea to use C#-compatible identifier names most of the times, because it will make the code readable to a wider audience - such as C# developers.
Is it possible to make Visual Studio to display tooltips on operators?
The following image demonstrates a tooltip hint for a function, but it does not work for operators.
Operators usually have simple type specs like 'T -> 'T -> 'T, but such hints can be useful for custom ones.
Following Daniel's suggestion, I'm posting a workaround that I've been using for myself.
The workaround is only partially helpful, and I'm still looking for any better ideas.
let (!><) a = ()
let z1 = op_BangGreaterLess 5
This code is fully valid, since an operator expression generates a function with a compiler-generated name. See this MSDN article, section "Overloaded Operator Names" for complete list of operator names.
Good news is that op_BangGreaterLess supports IntelliSense hints and it also supports "Go to Definition" (F12) command of IDE, pointing to an original operator declaration.
Bad news is that IntelliSense does not allow rapid entry of the full operator name (Ctrl+Space), so you have to type the entire name manually.
I'm afraid this is not possible (and even in Visual Studio 2012, I don't get tooltips for operators).
I suppose this could be implemented, but as you say, operators usually have simple types. When using custom operators, these should be probably simple enough so that people can use them without looking at their type (or the associated XML documentation). Otherwise, it might be better to use a named function.
That said, if you're using F# Interactive, then you can easily use that to explore the operator type:
> (!><);;
val it : ('a -> unit) = <fun:clo#2>
If I cannot use F# Interactive, I usually define a simple dummy symbol to get the IntelliSense:
let dummy () = (!><)
Note that I added unit argument to define a function and avoid value restriction error.
Is it possible to search or filter intellisense in visual studio?
Basically i know there is an enum in the project that contains 'column', but the enum doesnt begin with 'c'.
There has been lots of times where id rather not scroll through the hundreds (if not thousands) of valid objects it gives me.
I wonder if the real answer here is (and I won't be surprised to be voted down for this) that your enum isn't properly named. If it was then I'd expect the name to be obvious in the use context, may be consider renaming the enum?
You can search in Class View. Type "column" and hit enter.
Visual Studio 2010 changes all of this, giving you multiple very easy ways to do this type of search quickly.
If you're using ReSharper, you can use "Go To Symbol..." and type "column", and it will give you all symbols (types, properties, fields, methods, etc) that match.
Otherwise your best bet is to use the Object Browser and search.
I really don't know about doing that in intellisense itself, but assuming the objective is to actually find a member whose name you don't remember, you can write a small utility for that purpose using the underlying mechanism intellisense uses, reflection.
Open the Object Browser under View menu. From there, you can search within all the language constructs available to you.
Is it possible to get Visual Studio to do mathematical expression evaluation/reduction?
For example if I type '-0.005 + -0.345' how do I get Visual Studio to reduce that (i.e. replace it with the reduction)? Do I have to write a macro? If so, are there any pre-existing macros to do this type of expression reduction?
Just to be clear, I want to be able to highlight an expression and have it replaced with the reduced result. Many are suggesting the immediate window but I fail to see how that will suffice?
Edit I should point out that this is while editing not running or debugging. The immediate window is of little to no use. I also consider this a language neutral question. I would certainly be interested in seeing alternative macros to the one I had posted.
Edit Going Once... Going Twice... (i.e. any other suggestions before I consider accepting my own answer?)
Thank you for the above answers.
There probably are better ways, but here's a quick and dirty macro that does what I need.
References to the System.Data and System.XML namespaces need to be added.
Highlight the expression you want to evaluate and run the macro (it uses the calculated column in the DataTable to evaluate the expression.) It will replace the expression with the reduced result.
Edit - Updated code below. It worked extremely well for reducing a large number of expressions. As pointed out by others there is the immediate window but this will not work for editing purposes. This macro is a language independent solution for basic expressions "(), +, -, *, /".
Sub Eval()
Dim ts As EnvDTE.TextSelection = DTE.ActiveDocument.Selection
Using dt As New DataTable()
dt.Columns.Add("Expression", GetType(Double), ts.Text)
dt.Rows.Add(dt.NewRow)
ts.Text = CDbl(dt.Rows(0).Item("Expression"))
End Using
End Sub
Visual Studio by default will not do any mathematical expression evaluation / reduction. I'm not sure if you can get support for that via items like ReSharper, but if it is available it will be in an add-in.
Also, it would be helpful to know the language you are working in?
Some languages may be helpful in this area. F# for instance makes it easy to evaluate expressions in the IDE via the interactive window and will display out the result. This could easily be added back into your code but it doesn't appear to be exactly what you're looking for.
Here's an answer: Yes, it is possible using the following steps. (While technically performing what you're asking for, I'm not sure it will be extremely useful. :-)
Set a breakpoint in your program that's likely to get hit when you debug the program.
Then, run your program under the Visual Studio debugger.
When the breakpoint is hit, open the Watch window.
In the Watch window, add a new watch by clicking in the Name column.
Enter your expression '-0.005 + -0.345' (without the quotes) then hit [Enter].
... You should see the Value column get populated with -0.35.
Of course, that isn't in the context of the editor window ... which is, presumably, where you'd want to perform the reduction. So again, not very useful, I imagine. An add-in is the likely way to do that in the editor window.
You could just go to the immediate window and type "?<yourExpression>"
Are there any tools that augment/replace intellisense with a search of for expressions that will result in a given type?
I'm thinking of the case where I need to get some particular object and I known the type and will recognize the name but don't know what sequence of objects to jump thought to get to it. Such a tool would do a breadth first search of all options and list anything that has the correct type.
This is an old question, I know, but I'm giving it a shot in case you didn't find anything. Intellisense has the Resolve option (from at least Visual Studio 2005), with which you type your particular object straight, and then with right-clicking you can choose Resolve, which will add the library required to the list at the top of your class.
Similarly, ReSharper might have something fancier for you.