Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
get {
                if (statusMessage.Message =!null)
                return statusMessage.Message; 
            }


I need to check if the status message is null before returning it. but when I check it it give me the following error. then How can i check if its null or not?

StatusMessage.Message cannot be assigned to -- it is read only<br />
	<br />
Error	3	Cannot implicitly convert type 'bool?' to 'string'	<br />
Error	4	Cannot implicitly convert type 'string' to 'bool'
Posted
Updated 19-Oct-11 2:24am
v2
Comments
Xeshan Ahmed 19-Oct-11 8:28am    
by the way u wrote wrong code
get
{
if (statusMessage.Message !=null)
return statusMessage.Message;
}
this is error free now
BillWoodruff 19-Oct-11 9:07am    
In addition to the obvious typo of using '=!' instead of '!=' in the OP ... I would note that in the solutions proposed below (some of which will never compile because the 'get does not return a value in every case) ...

There's an assumption that StatusMessage.Message returns a string ... while that does seem like the most likely scenario here ... I'd note it's not the only one possible.
Simon Bang Terkildsen 19-Oct-11 10:53am    
Very true, the OP should have posted the entire property signature.

You're assigning !null (which doesn't compile) to statusMessage.Message You need to test statusMessage for null and then statusMessage.Message for null
C#
if (statusMessage != null)

The ! has to be on the left side of the =

Oh and one more thing you need to return something else in the case when statusMessage is null
C#
get
{
    if (statusMessage != null)
        return statusMessage.Message; 
    return string.Empty; // assuming statusMessage.Message is a string

}


Assuming statusMessage is never null you would do it like so
C#
get
{
    if (statusMessage.Message != null) // If statusMessage can be null it must be tested aswell
        return statusMessage.Message; 
    return string.Empty; // assuming statusMessage.Message is a string

}

Remember you have to return some other value in the case when statusMessage.Message is null.
And the ! has to be on the left side of the =
 
Share this answer
 
v7
Comments
pratheep7 19-Oct-11 9:06am    
why its syaing not all code path return a value.
Simon Bang Terkildsen 19-Oct-11 10:41am    
because you have to return a value in the case when your if statement is false as I said in my solution. If you use the getter I posted it will work, assuming you've fixed the type of the property.
Espen Harlinn 20-Oct-11 5:20am    
Good reply :)
Simon Bang Terkildsen 20-Oct-11 7:23am    
Yeah it ended out as acceptable, but started pretty bad, I miss read the question multiple times :( edited 6 times :D
simply use
C#
if(string.IsNullOrEmpty(statusMessage.Message))
return statusMessage.Message; 
 
Share this answer
 
Comments
Simon Bang Terkildsen 19-Oct-11 8:27am    
and what if statusMessage is null which is the OP's problem. And why not return null or an empty string? what would you return instead?
Though I fell into that trap as well, the OP is testing statusMessage.Message but needs to test statusMessage
Xeshan Ahmed 19-Oct-11 8:30am    
it will depend upon the logic of programmer i just solved his problem
Simon Bang Terkildsen 19-Oct-11 8:38am    
You're completely right I completely misunderstood the core of the question, multiple times :)
Xeshan Ahmed 19-Oct-11 9:57am    
its ok Simon Bang Terkildsen
Simon Bang Terkildsen 19-Oct-11 10:42am    
Thank you, Xeshan :)
Use != instead of =!.
C#
if (statusMessage.Message != null)
 
Share this answer
 
Comments
Shmuel Zang 19-Oct-11 8:55am    
That's the answer. You got my 5.
Toniyo Jackson 19-Oct-11 9:02am    
Thanks Shmuel :)
statusMessage.Message is string
and you are returning statusMessage.Message in a bool property
change the datatype bool to string of the property
and then use the following
C#
get {
         if (!string.IsNullOrEmpty(statusMessage.Message))
               return statusMessage.Message;
    }
 
Share this answer
 
Comments
Simon Bang Terkildsen 19-Oct-11 10:43am    
wont compile you have to return some value in the case when !string.IsNullOrEmpty(statusMessage.Message) is false
sachin10d 20-Oct-11 2:13am    
get {
if (!string.IsNullOrEmpty(statusMessage.Message))
return statusMessage.Message;
return null;
}
Just another way is to use the ?? operator[^].
C#
get
{
  return statusMessage.Message ?? string.Empty;
}


It returns the string in statusMessage.Message if it is not null. If statusMessage.Message is null, it returns string.Empty.
 
Share this answer
 
the best practice to check a string is null or not
is to use:
C#
if(string.IsNullOrEmpty(StringVariable))
   {
     // some thing to do if it is null

    }

you cant not return a string variable with null value
from a property that returns a string data type
so you can do like this :

C#
get
{
if(!string.IsNullOrEmpty(StringVariable
 return StringVariable;
return string.Empty; //the empty string or return another error string

}





if you want to check an object if is null or not try:

C#
if(obj!=null)

or
C#
if(obj==null)
 
Share this answer
 
if u are using a property type, then use [Serializable] key and write the above code

then it works
 
Share this answer
 
Comments
Simon Bang Terkildsen 19-Oct-11 10:46am    
huh? where do the SerializableAttribute come into the picture? it will have no effect what so ever on the OP's problem, all SerializableAttribute does is telling anyone, who is interested, that a type is serializable.
I think you are using property only with get attribute please assign the set attribute as well so that it will work for you syntax is given
set { Message = value; }
 
Share this answer
 
Comments
Simon Bang Terkildsen 19-Oct-11 10:47am    
The error message the OP received "StatusMessage.Message cannot be assigned to -- it is read only" indicates that StatusMessage.Message is read only, this you cannot give it a value like you suggest.

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