How to check if variable integer is empty?

Posted on 2016-09-16
Last Modified: 2016-09-21
In this class..

Person pr = new Person();

For string I use this

{pr.Lastname = lastname;}

Open in new window

What if it is an integer? for example  variable PersonID. What to use instead of !String.IsNullOrEmpty...
Question by:zachvaldez
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 22

Expert Comment

ID: 41802072
unless specifically nullable integers will always have a value.

if it is nullable then you can check the .HasValue property
if it is a db read you can check agains DbNull

Author Comment

ID: 41802088
I got this..
int PersonID = Convert.ToInt32(lblPerson.text);
LVL 22

Expert Comment

ID: 41802096
i believe if it fails it will return -1 you can use Int32.TryParse with an out value that you can check
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 22

Expert Comment

ID: 41802100
i was wrong about the -1.. you will get an exception. tryparse is your best bet.
LVL 43

Accepted Solution

zephyr_hex (Megan) earned 500 total points
ID: 41802360
Correct, you need to use TryParse();

int PersonID;
if (Int32.TryParse(lblPerson.ToString(), out PersonID))
	//parse was successful ...PersonID has the integer assigned

Open in new window


Author Closing Comment

ID: 41807298

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question