• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 94
  • Last Modified:

How to check if variable integer is empty?

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
zachvaldez
Asked:
zachvaldez
  • 3
  • 2
1 Solution
 
p_davisCommented:
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
 
zachvaldezAuthor Commented:
I got this..
int PersonID = Convert.ToInt32(lblPerson.text);
0
 
p_davisCommented:
i believe if it fails it will return -1 you can use Int32.TryParse with an out value that you can check
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
p_davisCommented:
i was wrong about the -1.. you will get an exception. tryparse is your best bet.
0
 
zephyr_hex (Megan)DeveloperCommented:
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
 
zachvaldezAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now