Click here to Skip to main content
15,887,946 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all,
this is supriya.I have a problem .In my code a function returns an object(User Defined).But i don't Know how to check whether this Object is empty or null.
My code is
SQL
txtAgeSex.Text = 
           objCommonNSBussinessObjects = objClinicalSummaryPresenter.getData(pIPno, gappln_data);
           objectToScreen(objCommonNSBussinessObjects);


private void objectToScreen(CommonNSBussinessObjects objCommonNSBussinessObjects)
        {
            txtMRNo.Text = Convert.ToString(objCommonNSBussinessObjects.mrno);
            txtRoom.Text = Convert.ToString(objCommonNSBussinessObjects.Room);
            txtWard.Text = Convert.ToString(objCommonNSBussinessObjects.Ward);
            txtAdmDate.Text = Convert.ToString(objCommonNSBussinessObjects.AdmissionDate);
            txtDocCode.Text = Convert.ToString(objCommonNSBussinessObjects.DocCode);
            txtDocName.Text = Convert.ToString(objCommonNSBussinessObjects.DocName);
            txtPatName.Text = Convert.ToString(objCommonNSBussinessObjects.PatNAme);
            txtAgeSex.Text = Convert.ToString(objCommonNSBussinessObjects.AgeSex);

        }

Here
objCommonNSBussinessObjects
is an object returned by getData(.....) method.
sometimes getData(...) returns Null Value.

i am getting an error
Object reference not set to an instance of an object.


help me in Cheking the object is empty or not
Posted
Updated 4-Sep-11 20:46pm
v2

There is no such concept as "empty" for an arbitrary type; and not even "null". The object can be compared to null if:

1) This object is of the reference class, see http://msdn.microsoft.com/en-us/library/t63sy5hs%28v=VS.100%29.aspx[^];

or

2) This object is of nullable type, see http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx[^].

Looks, before programming anything, it's advisable to read the language and .NET manual. These facts are quite elementary.

—SA
 
Share this answer
 
Comments
Suresh Suthar 5-Sep-11 2:58am    
Nice!!
Sergey Alexandrovich Kryukov 5-Sep-11 3:05am    
Thank you, Suresh.
--SA
Abhinav S 5-Sep-11 3:00am    
Yes correct.5.
However the OP can use null coalesce or a null check to avoid the error.
Sergey Alexandrovich Kryukov 5-Sep-11 3:06am    
Thank you, Abhinav.
Using coalesce is a good syntactic sugar, good idea.
--SA
raj ch 5-Sep-11 3:29am    
Thanks Abhinav. U saved me alot
One way to avoid the error could be to use the null coalesce operator - ??.
e.g.
C#
txtMRNo.Text = Convert.ToString(objCommonNSBussinessObjects.mrno??string.empty);
txtRoom.Text = Convert.ToString(objCommonNSBussinessObjects.Room??string.empty);


Another way would be to compare your values against null.
C#
private void objectToScreen(CommonNSBussinessObjects objCommonNSBussinessObjects)
        { if (objCommonNSBussinessObjects != null){
              txtMRNo.Text = Convert.ToString(objCommonNSBussinessObjects.mrno);
              txtRoom.Text = Convert.ToString(objCommonNSBussinessObjects.Room);
              txtWard.Text = Convert.ToString(objCommonNSBussinessObjects.Ward);
              txtAdmDate.Text = Convert.ToString(objCommonNSBussinessObjects.AdmissionDate);
              txtDocCode.Text = Convert.ToString(objCommonNSBussinessObjects.DocCode);
              txtDocName.Text = Convert.ToString(objCommonNSBussinessObjects.DocName);
              txtPatName.Text = Convert.ToString(objCommonNSBussinessObjects.PatNAme);
              txtAgeSex.Text = Convert.ToString(objCommonNSBussinessObjects.AgeSex);
            }
        }
 
Share this answer
 
Comments
Suresh Suthar 5-Sep-11 2:53am    
Nice answer.
Abhinav S 5-Sep-11 2:59am    
Thanks Suresh.
Philippe Mori 5-Sep-11 9:56am    
null coalesce operator (??) is not always usable. In your first example, if objCommonNSBussinessObjects is null then the exception would be thrown before ?? operator get executed.
Abhinav S 5-Sep-11 13:23pm    
Good point.
C#
if (objCommonNSBussinessObjects != null)
{
  //code 
}
 
Share this answer
 
v2
Comments
Abhinav S 5-Sep-11 2:59am    
Similar to my answer. 5. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900