I try to use AIP
public int AIP_NoSet
{
get { ;}
}
Compiler say that it is an error:
Program.c1.AIP_NoSet.get': not all code paths return a value
But even if I write
public int AIP_NoSet
{
get { ;}
set { ;}
}
it shows me the same error.
Where am I wrong?
You should write
public int AIP { get; set; }
when you write { ;} after it, this is seen as a syntacticaly incorrect attempt of a user implemented property. You can't have no setter, but you can make the setter private:
public int AIP_PrivateSet { get; private set; }
A moment of derp.
public int AIP_NoSet { get; set; }
Sounds like you want an automatic property with only a 'get' defined.
This is not allowed by the compiler.
You can accomplish this by adding a private set (as others have answered), or by not using an automatic property:
private int _aip = int.MaxValue;
public int AIP_NoSet { get {return _aip;}}
Or, if you NEVER want to set it, just use a const:
public const int AIP_NoSet = 2;
Make setter access private and fix syntax.
public int AIP_NoSet { get; private set; }
Related
I am geting data with the help of get and set methods. I want to calculate waste value. This is how I have tried to solve this issue.
class OSSP
{
private decimal waste;
public int DocNum { get; set; }
public string U_ItemCode { get; set; }
public string U_ItemName { get; set; }
public string U_ItemDesc { get; set; }
public string U_WetProcess { get; set; }
public string U_Color { get; set; }
public string U_Size { get; set; }
public decimal U_knitgWeight { get; set; }
public decimal U_FinishWeight { get; set; }
OSSP ossp ;
public decimal Waste
{
get => waste;
set
{
waste = ossp.U_knitgWeight - ossp.U_FinishWeight;
}
}
}
since you want Waste to be calculated from values of other properties and you want to have this value when you try to access this property you need to put the calculation code into the getter.
public decimal Waste
{
get => this.U_knitgWeight - this.U_FinishWeight;
}
furthermore it doesn't make sense to allow this value to be set, because it's meaning is based on the calculation of an internal state that is not visible outside. So that means that you can remove the setter entirely. By doing so you can also remove the private decimal waste; field because it is superfluous now.
EDIT:
One more thing that I notice is that you have an internal OSSP ossp; field, which is never set anywhere in your code. I assume you want to calculate with the actual values of the current object. So in this case you need to use this. before the 2 properties which you use for calculation.
Correct me if I am wrong here, because you might have purpose as why you made this OSSP ossp; field
get
{
return ossp.U_knitgWeight - ossp.U_FinishWeight;
}
When get method gets called it returns value after calculating.
This is my logs table:
public class Logs
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public DateTime Date { get { return DateTime.Now; } }
public string Controller { get; set; }
public string Action { get; set; }
public string Text { get; set; }
public bool isError {
get {
return this._isError.HasValue ? this._isError.Value : false;
}
set {
this._isError = value;
this.isError = value;
}
}
private bool? _isError = null;
}
and I don't know why when I try to make an insert, like:
var log = new Logs();
log.Action = "Send";
log.Controller = "Home";
log.Text = msg;
_context.logs.Add(log);
I get this error:
System.InvalidOperationException: 'Nullable object must have a value.'
And this is the object:
Except that private _isError variable, all the database columns have a value.
Where is the error?
Why is your private property nullable and your public property is not?
I would remove your private property and then change your public property:
public bool IsError { get; set; }
Since you're never going to return 'null' anyway, why would you make it an option?
Edit:
I also think this will cause an endless loop of your IsError property to be set:
set {
this._isError = value;
this.isError = value;
}
You're setting the public property within the public property setter.
Only setting the private property here is enough since you return the value of the private property in your getter.
This is just a side note though, I would still remove the private property and just use
public bool IsError { get; set; }
I have a C# class with properties in which I end up using this class a a collection of type List in another class.
What I want to do is just always set the Type property to be of value "3"
Should /Can this be done with the getter/setter or should I use the System.Component.DefaultValue .... attribute
public class ReportDefinition
{
public int Id { get; set; }
public string ReportGroupNameDef { get; set; }
public int SortOrder { get; set; }
public int ReportGroupId { get; set; }
[System.ComponentModel.DefaultValue(3)]
public int Type { get; set; }
}
I think that I would prefer not using this way [System.ComponentModel.DefaultValue(3)]
You could use a read-only property, and either return the value of a private field, or just return the value you want right in the get.
public class ReportDefinition
{
private int m_type = 3;
public int Type
{
get
{
return m_type;
}
}
}
I don't expect to get or steal away the existing selected answer but I did want to help clarify a bit of the comments that I am reading.
Yes indeed best clean way is auto-property public int Type { get; } = 3; -- Caveat is C# 6
Another C# 6 without auto-property would be an expression body
private int m_type = 3;
public int Type => m_type;
However If you are stuck with wanting to use Linqpad 4 then the selected answer is "fine"
private int m_type = 3;
public int Type
{
get
{
return m_type;
}
}
I want have readonly property in a Data Transfer Object,DTO object, without set; accessor like:
public class ViewBannerDTO
{
public int Id { get; }
}
but why get:
'ViewBannerDTO.Id.get' must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors.
and also why i cant:
public readonly int Id{get;}
You can't have no setter for an auto-implemented property (otherwise how would you set it?). You can either add a getter implementation (and a backing field if necessary) or use a private setter:
public class ViewBannerDTO
{
public int Id { get; private set; }
}
Why i cant I do:
public readonly int Id{get;}
because readonly only applies to fields. You can accomplish the same thing with a property by using a readonly backing field and no set accessor:
private readonly int _Id;
public int Id {get { return _Id; } }
but you can't have a readonly auto-implement property because there's no syntax to initialize a property without a set accessor.
It is exactly what is sais: There is not set accessor for that variable and you have no Get method implemented which can do stuff to get you a value.
Either go:
public int Id { get; set; }
OR
public int Id
{
get
{
int something = GetStuffDone();
return something;
}
}
Another something you can do is make the set function private like this:
public int Id { get; private set; }
And an answer to why you cant: The value will never be set cause it has no accessor.
This is just a repeat of answers but OP does not understand
public class ViewBannerDTO
{
public int Id { get; private set; }
public ViewBannerDTO ()
{
Id = 12; // inside the class can assign private
// private not seen outside the classs
}
}
or you could
public class ViewBannerDTO
{
private int id = 12;
public int Id { get { return id; } }
}
or you could
public class ViewBannerDTO
{
public int Id { get { return 12; }
}
As of C# 9 you can get read-only behavior by using an init accessor. Example:
public class Foo
{
public int Bar { get; init;} = 1
public int Baz { get; private init;} = 2
}
var foo = new Foo { Baz = 3};
In both cases the property can only be set during object construction. The private keyword ensures only the class can set the value, otherwise the caller of new can set the value with the object literal notation in the example.
reference: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/init
How do I initialize the member variables during declaration and create the getter/setter shorthand? Is it possible or do I have to use the constructor to assign the value?
For example I want to do something similarl to this
public class Money
{
public int dollars = 200 {get; set;}
}
or
public int dollars = 200;
dollars
{
get;
set;
}
In C# 6 and later, you can initialize auto-implemented properties similarly to fields:
public string FirstName { get; set; } = "Jane";
Source: MSDN
Either
public class Money
{
private int dollars = 200;
public int Dollars
{
get { return dollars; }
set { dollars = value; }
}
}
or
public class Money
{
public int Dollars { get; set; }
public Money()
{
Dollars = 200;
}
}
Unfortunately there is currently no way to achieve this.
You must either assign the default value when you declare the properties backing field or assign the default value from the constructor if you are using an automatic property.
public class Money
{
public int Dollars {get;set;}
public Money()
{
Dollars = 200;
}
}