does anyone know how i can get an indentation after pressing enter on a open brace?
Instead of:
public void SomeFunction()
{
[cursor here]
}
this here:
public void SomeFunction()
{
[cursor here]
}
So after typing the open { i don't want to have the same indention as the brackets but be indented by one tab
I ran into this earlier today. I found out that it's not ReSharper, but Visual Studio. Solution here: Visual Studio 2013 Indentation Not Working While Writing
Related
I am trying to format C# code of a WinForms .NET Core 7.0 project in Visual Studio Community:
private void Form1_Load(object sender, EventArgs e)
{
Dictionary<String, String> Dictionary = new Dictionary<String, String>
{
{ "operation", "login" },
{ "phone", "123"},
{ "country","456"} ,
{ "otp", "789"},
{"language","111" }
};
}
I have tried Ctrl+K, Ctrl+D, removing the last brace in the code and putting it back.
The extra spaced are not getting removed. Can these be removed in whole code automatically using a command? If not, is there a plugin / extension that can help in code formatting?
Use the shortcut key Ctrl+f.
Enter [^\S\r\n]{2,} in FIND
The value in the "replace" is empty
Select use regular expression(Alt+E)
Click the Replace All button(Alt+A)
Use the Auto Align shortcut (CTRL+K+D)
ReSharper works with VS Community and will reformat that case by either
removing the ; and re-entering it (format on typing)
or reformatting the whole file via Cleanup Code... on the context menu for the C# file.
You'd have to pay for ReSharper, though, unless you're a student, working on an open-source project, or otherwise qualify for the free license.
Visual Studio Community (and probably Pro and Enterprise) doesn't seem to reformat dictionaries regardless of what you seem to do with a .editorconfig file or what you have set in Tools | Options | Text Editor | C# | Code Style | Formatting | General.
There may be other extensions that do this. Perhaps you could find one on the Visual Studio Marketplace.
Image of my code in Visual Studio
Forgive me, I am somewhat new to coding, so maybe this is a dumb question.
I am learning how to use Unity and I am using Visual Studio to edit my code (C#).
For some reason, the red squiggly lines will not appear for most errors. It won't even recognize that there is an error.
The only error I have had it recognize is a missing semicolon.
For example, I can say a string is equal to a float (as seen below: characterName = itemDurability;), and it had not issue with this in either Visual Studio or VSCode. I can have an item defined as multiple different things, and still no errors. Of course Unity will tell me there is a problem when the code loads, but I'd like to know as I'm writing it.
I have tried updating and reinstalling, but nothing works. I cannot find anything online that has helped me with this problem.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
string characterName;
characterName = "Duncan";
int characterLevel = 5;
int experience = characterLevel * 5;
float itemDurability = 1.527f;
experience = (int)itemDurability;
characterName = itemDurability;
bool equippable = false;
if (itemDurability > 1f)
{
experience = (int)(itemDurability * 1.5f) / characterLevel;
}
}
}
From your screen-shot, it appears that you have opened the .cs (using VS) by double clicking on it or that file is not part(or excluded) of your Project , essentially you're opening the file for editing and using VS like a text editor as Indicated by Miscellaneous Filesand Attach Debugger symbol from your snap
Ensure that File is added to your Project with SolutionExplorer window and BuildAction=C# Compiler. See example
The only error I have had it recognize is a missing semicolon.
Visual Studio IDE interpreted the file as C# from the .cs extensions, and because of which it will look for ; and a semicolon is required at the end of every statement in C#.
For some reason, the red squiggly lines will not appear for most errors
Compiler is only doing a syntax check and not checking the semantics of the statements, for that matter compiler will even accept something like
Myclass a = new MyClass(); even though MyClass is not defined
So let's say I want to encapsulate the field with the good ol' Edit->Refactor->Encapsulate field, since it saves quite a bit of time:
private GameSettings gameSettings;
In Visual Studio 2015, I would get:
public GameSettings GameSettings
{
get
{
return gameSettings;
}
set
{
gameSettings = value;
}
}
But with Visual Studio 2017 I get:
internal GameSettings GameSettings { get => gameSettings; set => gameSettings = value; }
Is there any way I can make it generate the old style? It looks wrong to have half the properties in one style and half in another...
I know this thread is old, but the answer can help anyone else...
You can go to Options > Text Editor > C# > Code Style > General and change "Use expression body for accessors" to "Never". So you'll get the old style.
Try messing around with the snippets:
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC#\Snippets\1033\Refactoring\EncapsulateField.snippet"
First off all, sorry if this question already exist but my english is limited for this task.
I use Visual Studio on C# and of course, IntelliSense.
When I write a new method, an If or any other "method/constructor", IntelliSense write like that (when I press Tab or Enter to validate the suggestion)
Actual result :
if (true)
{
// code
}
private void Test()
{
// code
}
// ...
What I want :
if (true) {
// code
}
private void Test() {
// code
}
// etc. ...
See what I want to do ?
I prefer the second 'theme', most readable for me and row economizer.
Is an option behind, hidden in the dark side of VS to do that ?
Thanks for help.
Seems you're asking for javascript style formatting here. I found this link that might help.
1.Click Tools | Options…
2.Scroll down to the Text Editor node
3.Expand the C# node
4.Expand the Formatting node
5.Click on the New Lines node
6.You will see a list of options like in the image below which give you full control over when Visual Studio should put your open brace on
a new line
You can set that on Options window:
Tools -> Options -> Text Editor -> C# -> Formatting -> New Lines
I'm writing a VS Package where I need to store the time when the user start my package, and when the user close the Visual Studio.
The problem is, I don't know how to get the closing event for the Visual Studio. Can anyone give me any information about how to detect if the VS is closing?
Note: When I search from the internet, I got the following similar problem to mine: How do you cancel a ToolWindowPane or Visual Studio IDE close operation via a VSPackage?, but when I try it, this solution is to detect and do something when the Package window is closed, and cannot detecting when the Visual Studio is closed.
Any help is really appreciated.
Thanks
Just to make it explicit and close this problem.
This is the snapshot of code to check if VS is closing:
// Create 2 variables below
private DTE2 m_applicationObject = null;
DTEEvents m_packageDTEEvents = null;
Then in Initialize add this:
// Link the Event when VS CLOSING
m_packageDTEEvents = ApplicationObject.Events.DTEEvents;
m_packageDTEEvents.OnBeginShutdown += new _dispDTEEvents_OnBeginShutdownEventHandler(HandleVisualStudioShutDown);
Two other methods that you need:
public DTE2 ApplicationObject
{
get
{
if (m_applicationObject == null)
{
// Get an instance of the currently running Visual Studio IDE
DTE dte = (DTE)GetService(typeof(DTE));
m_applicationObject = dte as DTE2;
}
return m_applicationObject;
}
}
And
public void HandleVisualStudioShutDown()
{
MessageBox.Show("Exiting Visual Studio. Bye");
}