Visual Studio 2015 encapsulate field adds to wrong line - c#

In VS2015 when I encapsulate field
private AdminAccountDetailsDC _Profile;
I get this result
public AdminAccountDetailsDC Profile
{
get
{
return _Profile;
}
set
{
_Profile = value;
}
}
But it's added to random line, just in the middle of file. Comparing to VS2012 - result was added right after private field. Is it possible to correct that and my property is added right below field?

Related

Style of generated properties Vs C#

How can I change the style of the properties that are generated by Visual studio?
This is what I get :
public int Population {
get {
return _Population;
}
set {
_Population = value;
}
}
This is what i want :
public string Population
{
get{return _Population;}
set{_Population=value;}
}
You are looking for existing propfull code snippet.
So type propfull and press Tab twice your desired format will be created.
private int _Population;
public int Population
{
get { return _Population; }
set { _Population = value; }
}
**Edited:**Control the way code is formatted after Refactor->Encapsulate Field
Please note that Language Service formats code once its added using code-snippets. Hence, one can change the formatting of the code inserted after Refactor->Encapsulate Field by changing option Leave block on single line as unchecked. Its shown in image below.

Auto Encapsulate Field Refactoring, Difference between 'Use field' and 'Use Property'?

On Visual Studio 2017, I have two options when using the Auto Encapsulate Field Refactoring Tool:
Use Property
Still use field
I have tested the different option on a basic class:
public class Test_EncapsulateFieldRefactoring_Property
{
public int id;
public string name;
}
But both option gave the same result:
public class Test_EncapsulateFieldRefactoring_Property
{
private int id;
private string name;
public int Id { get => id; set => id = value; }
public string Name { get => name; set => name = value; }
}
Why do those options exist? Where is the difference (in code generated , "useage"*)?
Disclamer:
The screenshot is a on French VS. So option translations are made by me, real option text may differ.
I know the difference between field and property. I have checked a lot of topics to see if it was not a dupe. I could have missed one.
*, Can't find a good translation for this one: "in the way you use it". But in this context not the difference in use between as field and property but in the menu option.
In English, the options are called:
Encapsulate field (and use property)
Encapsulate field (but still use field)
The difference is in what it does to usages of the field. The first option will update all usages of that field to use the new properties that it creates. The second option doesn't change existing usages of the field elsewhere in your code.
So if elsewhere you have this code:
var test = new Test_EncapsulateFieldRefactoring_Property();
test.name = "Hello";
You'll find that the first option updates test.name to the new test.Name property, but the second option doesn't.

Can't use public class in my web service

Why i can't use the following public class :
namespace OrganizerUI.App_code
{
public class Employee
{
private string text;
public string Text
{
get { return text; }
set { text = value; }
}
}
}
in my web service :
Often (always?), code files that are added to the Visual Studio App_Code directory are not set by default to compile even if they are .Net code. They are set to "Content" which means they are included in the output only. If you right-click the file and choose "properties" you can see/change it to "compile" instead of "content".

Sharepoint visual web part custom settings resets

I create in my sharepoint visual web part custom properties, problem is that after a server restart, the value disappears.
public enum Organ { INST1, INST2 };
public static Organ OrganEnum;
[Category("Custom settings"),
Personalizable(PersonalizationScope.Shared),
WebPartStorage(Storage.Shared),
WebBrowsable(true),
WebDisplayName("Organ"),
WebDescription("Choice Organ")]
public Organ _OrganEnum
{
get { return OrganEnum; }
set { OrganEnum = value; }
}
I tried in sharepoint web.config edit this line, but it does not work
<SafeControl Assembly="WebPart, Version=1.0.0.1, Culture=neutral, PublicKeyToken=998d82b12e783432" Namespace="WebPart.Organ" TypeName="*" Safe="True" SafeAgainstScript="True" AllowRemoteDesigner="True" />
Your property OrganEnum shouldn't be static. That is probably what causes your trouble.
Try decaring you property like this:
public Organ OrganEnum{ get; set; }
and skip the
public static Organ OrganEnum;
altogether.

Tag to edit DataMember of user control in visual studio designer

I have the [AttributeProvider (typeof(IListSource))] tag which lets me edit the DataSource field via dropdown list in the visual studio editor. Is there a similar tag to use so i can edit the DataMember property the same way. Right now i have to type in the value for DataMember which take a lot of time if i have to keep looking up the field names...
[AttributeProvider(typeof(IListSource))]
public object DataSource
{
get
{
return this.dataSource;
}
set
{
this.dataSource = value;
BindTextBox();
}
}
[Editor("System.Windows.Forms.Design.DataMemberFieldEditor", typeof(System.Drawing.Design.UITypeEditor))]
public String DataMember
{
get
{
return this.dataMember;
}
set
{
this.dataMember = value;
BindTextBox();
}
}
I ended up using [Browsable(true)]. this let me edit the field as a text field but no drop down menu...

Categories

Resources