Shortcut for autocompleting properties in visual studio? - c#

I was watching a video about C# and the guy used a shortcut to implement a property.
public decimal Price { get; set; }
He wouldn't write the whole line, but instead let the visual studio editor fill in the get and set. Does anyone know the shortcut for completing the last part of the line above? Or even better, like a pdf quick-reference overview?

In Visual Studio 2010 you can type 'prop' then press tab twice. It's probably the same in earlier versions.
Edit: You'll then have to change the type (defaults to int) and the name (defaults to MyProperty).
Edit2: I've just found this great tool that allows you to edit these code snippets to override their default behaviour, or create new ones: http://snippeteditor.codeplex.com/

Related

Sergen : syntax erros despite rebuilt

For one of my internship mission, I need to use a C# app-builder, serenity.is with Visual Studio 2015. I'm following this official tutorial.
More precisely, I followed the begin of the tutorial p 43. (I'm just adapting it to what I do, I join the code at the end of the post).
Nothing is going wrong till p 49. The code generator of the app-builder, sergen.exe, is creating some code (I don't have the detail of what he creates but it doesn't seem to be important).
They ask me to "rebuild all", what I did, and everything should work smoothly.
As project is modified, Visual Studio will ask if you want to reload changes, click Reload All.
REBUILD the Solution and then press F5 to launch application.(tutoriel)
Nevertheless, when i compile and execute my code, I've got a bunch of syntax error which shouldn't happen.
You can find the code of my migration file below, but I don't think it is the problem.
using FluentMigrator;
using System;
using FluentMigrator.Infrastructure;
namespace Serene3.Migrations.DefaultDB
{
[Migration(20170802070000)]
public class DefaultDB_20170802_070000_TcpDump : Migration
{
public override void Up()
{
Create.Schema("tcpdump");
Create.Table("TCPDump").InSchema("tcpdump")
.WithColumn("TimeStp").AsString(16).Nullable()
.WithColumn("IdTransmission").AsInt32().Identity().PrimaryKey().NotNullable()
.WithColumn("IdSource").AsString(32).Nullable()
.WithColumn("IdDestination").AsString(32).Nullable()
.WithColumn("PortSource").AsString(16).Nullable()
.WithColumn("PortDestination").AsString(16).Nullable()
.WithColumn("-->").AsInt32().NotNullable()
.WithColumn("<--").AsInt32().NotNullable();
}
public override void Down()
{
}
}
}
I tried to stay as close as the tutorial as possible. I probably forgot to do something, but I can't find what.
Is there any Serenity user which could help?
Feel free to ask any other details
Make sure that all column names are supported by the tool. Column names like "-->" and "<--" would need special syntax in SQL and can't be used as property names in generated code files.
Use property-ready names without special characters or spaces. If you need special names, consult the tools documentation and make sure to use supported techniques.

C# Visual Studio 2013 suppress 'Class is never instantiated'

I have a web api project which accepts HttpPost communications.
The controller's methods always accepting a single validated object.
For example:
public sealed class NumbersRequest
{
[NumberOne]
public string Number1 { get; set; }
[NumberTwo]
public string Number2 { get; set; }
}
Since I never declare NumbersRequest req = new NumbersRequest() and they only serve as a request object, Im getting the
class is never instantiated
How can I suppress the warning? (its more like a green underline..)
Maybe something with annontations?
Thanks.
This looks like a ReSharper warning and as such you can ask ReSharper to be silent about these things.
You can either configure ReSharper to stop complaining about this overall, you do this simply by hitting Alt+Enter on the squiggly in question and use the bottom menu item that usually allows you to configure the inspection severity.
You can opt to save this in your global settings, which means it will affect every project you open from now on, or you can save it to a team-shared settings file which you can then check into source control alongside your project, to make it only count for this one solution.
Now, if you want to keep the warning overall but ask it to stop complaining about one or more particular types, methods, properties or the likes, you can use the attributes that ReSharper provides.
You have several ways of bringing these attributes into your project:
Add a reference to the Nuget package "JetBrains ReSharper annotations"
Use the options dialog for ReSharper and find the page where it allows you to grab a copy of the source for those attributes onto the clipboard, then simply paste this into a file in your project.
Define just the one or two attributes you want, even in your own namespace (which you then have to tell ReSharper about)
The recommended way is option 1, use the nuget package.
Assuming you now have the attributes available you can use either PublicAPIAttribute or the UsedImplicitlyAttribute.
Either one should suffice but they may have different connotations. Since you're flagging objects being transferred to or from clients I would go with the PublicAPIAttribute first.
Since you say in a comment that the PublicAPIAttribute didn't work but UsedImplicitlyAttribute did then I guess they do have different meanings.

How to specify order of debugger visualizers in Visual Studio

I've been working on a debugger visualizer for Visual Studio for some time and while the actual visualizer works fine. The problem is that it always places itself at the top of the visualizer list when examining a variable which really annoys some of the users who rather have Text as the top one (since the top one is also default when opening VS).
I can't find any support for this on DialogDebuggerVisualizer or DebuggerVisualizerAttribute which were my first thoughts so I've been scouring SO/MSDN/Google for information on how to affect the sort order of the visualizers (preferably to put mine last in the list) but to no avail.
Below is how I register my visualizer, it then just shows a form based on the value that is being visualized.
using Microsoft.VisualStudio.DebuggerVisualizers;
[assembly: System.Diagnostics.DebuggerVisualizer(
typeof(Shorthand.VSAddins.JsonVisualizer.JsonVisualizer),
typeof(VisualizerObjectSource),
Target = typeof(string),
Description = "Json Visualizer")]
namespace Shorthand.VSAddins.JsonVisualizer
{
public class JsonVisualizer : DialogDebuggerVisualizer
{
protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
{
var json = objectProvider.GetObject() as string;
var form = new VisualizerForm { Json = json };
windowService.ShowDialog(form);
}
}
}
Does anyone know if it is possible to affect the order of the visualizers or should I just let it be?
I don't think there is a solution. But there is a workaround:
Define your own Text Visualizer and put appropriate DebuggerVisualizer attribute before the attribute of your JsonVisualizer. The result will be that string will be readable by default and Json Visualizer can be chosen. A window with a multi-line textbox is not too much work.
It is probably not even necessary to write visualizer. It should be possible to use internal one but I don't know its name (Which class is used for "Text Visualizer"?).
It will always appear first, by design. The under the hood cast has found the best match for the variable it is reflecting on.
however, you could do either of two things. You could make the visualizer only appear when the sting contains ':'
Or you could use reflection to reorder the visualisers by adding them to the end of the collection in the order you want, then removing the originals from the collection.
For the latter you will most likely have to change the collection from readonly to writable. Via reflection.
There is no reliable source to draw on other than your will to succeed.
I guess that VS 'under the hood' can distinguish between type of string and type of xml quite easily, but Xml is just a string too, so a key question here would be, how does VS tell the difference between the two?
Could you dissect the VS XML visualizer to see how it works (even if you have to use reflector on the DLL to do it, you might get to see the method that works it out)

Change the Implement Interface template

In Visual Studio 2010, is it possible to change the default template used when implementing an interface?
I would like to change the implementation of properties from
public int MyProperty
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
to
public int MyProperty { get; set; }
Edit
so i tried to Edit PropertyStub.snippet but to no avail, it didn't change anything...
I found this question Changing property stubs for interface refactoring which says that ReSharper is the only way.
Anyone able to make this work, with example, cause the change i made did not seem to have any effects
Yes, it is possible to change this template. The C# IDE uses templates for the majority of it's code generation and the IDE. You can update these templates to control the code generation process. They are located in the following directory
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Refactoring
Note: The " (X86)" portion will be absent on 32 bit machines.
For this particular situation you want to change the PropertyStub.snippet file.

ReSharper Code Snippet for Auto-Property

Is there a way to create an auto property via code snippet using ReSharper? Like the prop for VS?
Resharper 5 has a prop snippet which goes to auto-property. Does 4.5 not?
You can create a Live Template
Just go to the Resharper menu, and choos Live Templates. Click the little green + icon and add this:
public $type$ $name$ { get; set; }
Then give the wanted shortcut, and you are good to go.
Yes. You can setup any snippet you wish. These are called "Live Templates" in Resharper, and have a lot more functionality than the VS versions. Just choose "Resharper->Live Templates.." in VS to edit them.

Categories

Resources