Solved

How to check if variable integer is empty?

Posted on 2016-09-16
6
50 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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 42

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

808 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