Solved

How to check if variable integer is empty?

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

Person pr = new Person();

For string I use this
 
 if(!String.IsNullOrEmpty(lastname))

{pr.Lastname = lastname;}

Open in new window


What if it is an integer? for example  variable PersonID. What to use instead of !String.IsNullOrEmpty...
0
Comment
Question by:zachvaldez
[X]
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
6 Comments
 
LVL 22

Expert Comment

by:p_davis
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
1
 

Author Comment

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

Expert Comment

by:p_davis
ID: 41802096
i believe if it fails it will return -1 you can use Int32.TryParse with an out value that you can check
0
Independent Software Vendors: 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

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

Accepted Solution

by:
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

0
 

Author Closing Comment

by:zachvaldez
ID: 41807298
thanks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

729 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