I'm validating some properties and I need to know if long or integer values are set by other layers.
For example, this class::
public class Person
{
public int Age {get;set;}
}
When I set a new instance of Person, the Age gets the value 0. But I have to validate if the Age were set, because the Age can be zero (not in this context of course).
One solution that I've thought of is to use the int as a nullable integer (public int? Age) and in the constructor of Person set the Age as null.
But I'm trying to avoid it because I would have to change too many classes just to check if Age.HasValue and to use it as Age.Value.
Any suggestions?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.
Int's are by default initialized to 0; Assuming you don't want to use int?
which would work perfectly for you. You can check against that or you can have a flag and a backing field:
private int _age;
public int Age
{
get { return _age; }
set { _age = value; _hasAge = true; }
}
public bool HasAge { get { return _hasAge; } }
As suggested above you can initialize it to an invalid state:
private int _age = -1;
public int Age
{
get { return _age; }
set { _age = value; _hasAge = true; }
}
public bool HasAge { get { return _age != -1; } }
Or just break down and use an int?
public int? Age { get; set; }
public bool HasAge { get { return Age.HasValue; } }
For backwards compatability with your code, you can back it off an int?
without exposing it:
private int? _age;
public int Age
{
get { return _age.GetValueOrDefault(-1); }
set { _age = value; }
}
public bool HasAge { get { return _age.HasValue; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With