Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Check if integer property is set

Tags:

c#

properties

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?

like image 854
Gui Avatar asked May 10 '11 16:05

Gui


People also ask

What C is used for?

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 ...

What is the full name of C?

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.

Is C language easy?

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.

Is C programming hard?

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.


1 Answers

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; } }
like image 89
Aren Avatar answered Oct 23 '22 04:10

Aren