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.
Related
My code is:
namespace RTT_API
{
class routepoint
{
public string description { get; set; }
public string TIPLOC { get { return TIPLOC; } set { SetStationRef(); } }
public string publicTime { get; set; }
public Guid StationRef { get; set; }
public void SetStationRef()
{
SqlCommand comStationRef = new SqlCommand("select uniqueref from station where tiploc=#tiploc", Globals.RTTConn);
comStationRef.Parameters.Add("#tiploc");
comStationRef.Parameters["#tiploc"].Value = TIPLOC;
SqlDataReader rdrStationRef = comStationRef.ExecuteReader();
if (rdrStationRef.HasRows == true)
{
rdrStationRef.Read();
StationRef = rdrStationRef.GetGuid(1);
rdrStationRef.Close();
}
else
{
rdrStationRef.Close();
comStationRef.CommandText="Insert into station(tiploc) values (#tiploc)";
comStationRef.ExecuteNonQuery();
}
rdrStationRef.Close();
}
}
}
I would appreciate help with the following:
If I try to debug the TIPLOC value I receive a Stackoverflow error. It worked OK when the definition was only:
public string TIPLOC { get; set; }
The SetStationRef method doesn't run when the value is set
The TIPLOC value is set by a JSON deserialise command. Is using a custom 'set' method the correct way to set the StationRef value? I tried putting code in the class constructor but it looks like the constructor runs before the JSON deserialise sets the properties? I'm new to C# so trying not to get into any bad practices to start with.
Thanks
This line
public string TIPLOC { get { return TIPLOC; } set { SetStationRef(); } }
will return TIPLOC indefinitely because it is calling itself. Calling return TIPLOC will call the getter of TIPLOC and its just going to keep doing that until it overflows the stack. When you debug the debugger will try and retrieve the value of the property and will get stuck in that loop.
set { SetStationRef(); } also ignores the value. I assume you want to call SetStationRef(value);. In the setter of a property the value the user is trying to set gets passed in as a local variable called value
From the overall look I think what you are trying to achieve is something along the lines of
private string tiploc;
public string TIPLOC
{
get
{
if(tiploc == null)
{
tiplock = GetValueFromDatabase(); // Replace this to retrieve the value
}
tiploc;
}
set
{
SetStationRef(value);
tiploc = value;
}
}
My original three questions were:
If I try to debug the TIPLOC value I receive a Stackoverflow error
The SetStationRef method doesn't run when the value is set -
The TIPLOC value is set by a JSON deserialise command. Is using a custom 'set' method the correct way to set the StationRef value? I tried putting code in the class constructor but it looks like the constructor runs before the JSON deserialise sets the properties? I'm new to C# so trying not to get into any bad practices to start with.
Question 1 was answered by Ben and his help enabled me to find a solution to questions 2 and 3 so a lot of credit is due to him.
I've only added a separate answer because the custom accessor needs to be defined on the StationRef and not the Tiploc (the Tiploc is set by the Deserialisation code) as follows:
public string tiploc { get; set; }
public Guid StationRef
{
get
{
if (stationRef == Guid.Empty)
{
stationRef = Globals.GetStationRef(tiploc);
}
return stationRef;
}
set
{
stationRef = value;
}
}
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?
I've got a bit of a strange situation going on. I've created a very simple class as such.
[Serializable]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
public class StockSyncCollection : ObservableCollection<StockSyncDatum>
{
}
public class StockSyncDatum
{
public StockSyncDatum() { }
public StockSyncDatum(int posStockId, int posStockCatalogueId, DateTime lastUpdated)
{
this.PosStockId = posStockId;
this.PosStockCatalogueId = posStockCatalogueId;
this.LastUpdated = lastUpdated;
}
public int PosStockId { get; set; }
public int PosStockCatalogueId { get; set; }
public DateTime LastUpdated { get; set; }
}
I have then created a custom setting through the designer for which the generated code looks like this
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public global::Sync.Config.StockSyncCollection StockSyncCollection {
get {
return ((global::Sync.Config.StockSyncCollection)(this["StockSyncCollection"]));
}
set {
this["StockSyncCollection"] = value;
}
}
However the problem I am running into is that later in my code (after adding some items to the collection) I will call
Settings.Default.Save();
But what happens is that only some subset of the items that have been added to the collection up until the point the save method is called is actually persisted. So for example, I might add 50 items but only 10 of them will be saved.
Now note that I am calling Settings.Default.Save() in a loop. My gut instinct is telling me that there is some sort of hash that is generated of the object that isn't being updated.
So what I am wondering is how the hell do you debug Application Settings in C# apps? I can see various events like SettingsChanging and SettingsSaving but I do not see an event for SettingsSaveError or similar.
Anyone have any idea what might be going on or how to debug this?
EDIT: The loop code basically looks like this.
foreach (var partition in stockTransfers.Partition(PartitionCount))
{
IList<StockTransferContract> stockTransferContracts = partition.ToStockTransferContracts();
//Do all the magic syncing stuff...
foreach (var item in stockTransferContracts)
_StockSyncCollection.Add(new StockSyncDatum(posStockId, posStockCatalogueId, DateTime.UtcNow));
Settings.Default.Save();
}
Keep in mind that I need to call save because I expect errors to occasionally arise and need to ensure that the previously "Sync'd" items are noted.
Quick points for someone who might know the answer - is there a snippet or tool that can quickly generate template code to assign all public fields and/or properties of an object?
Example:
public class SomeBloatedClass
{
public string SomeField1 { get; set; }
public int SomeField2 { get; set; }
// etc...
public string SomeField99 { get; set; }
}
public class TestHarness
{
public SomeBloatedClass CreateTestObject()
{
// Is there a snippet/macro/template that can generate the code to assign
// all public fields/properties so they can be manually assigned quickly?
// Something like this...?
// *Begin auto-generated code
SomeBloatedClass s = new SomeBloatedClass();
s.SomeField1 = ;
s.SomeField2 = ;
// etc..
s.SomeField99 = ;
// *End auto-generated code
return s;
}
}
Third-party tools are fine as long as they integrate into Visual Studio.
Edit: I'm just looking to have the tool create empty assignment statements that I could quickly hand-edit with the appropriate values. Ideally, the solution would use the built-in snippet mechanism to navigate from statement to statement via the TAB key - I couldn't represent that clearly using StackOverflow's editor, but if you've used snippets you should know what I mean).
There is small snippet or tool that can quickly generate template code to assign all public fields and/or properties of an object?
c# property assigner tool.
and behalf of it we can also generate properties of c# ex.( get set )
c# property generator
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...