Click here to Skip to main content
15,941,286 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
How to Check EMPTY (or) NULL Sting?

Using the below code I get an error.
VB
Dim strr As String
If (strr.ToString() = "") Then
       Response.Write("HAI")
End If

The error:
Object reference not set to an instance of an object.

Thanks in advanced
Posted
Updated 2-Mar-12 23:26pm
v5
Comments
André Kraak 3-Mar-12 5:27am    
Edited question:
Added pre tags
Spelling/Grammar

Firstly why on earth call ToString on a string? I get what you were trying to do but it really won't help. What you should do is:

C#
string str = null;
if(string.IsNullOrEmpty(str))
{
//do something
}


Hope this helps,
Ed
 
Share this answer
 
v3
Comments
rajrprk 3-Mar-12 5:01am    
here i m getting error like this
ie., 'IsNullOrEmpty' is not a member of string
Ed Nutting 3-Mar-12 5:17am    
Please see this MSDn page - it does exist: MSDN IsNullOrEmpty Method - it does exist just make sure you've remembered the using System; statement.
rajrprk 3-Mar-12 5:19am    
If (strr = Nothing) Then
'Something
End if
i have changed like this now it's ok
Ed Nutting 3-Mar-12 5:24am    
Yes but you haven't detected empty strings.... The IsNullOrEmpty method is far better and more convenient - I would suggest you try (if you are using .Net Framework 2.0 as you tagged you were) - If [String].IsNullOrEmpty(s) = True Then - this may help : on the MSDN page you must select which version of the framework you are using to get correct help. Anyway, hope this now works,
Ed
ProEnggSoft 3-Mar-12 6:54am    
I agree. This is the preferred method. Still better is String.IsNullOrWhiteSpace method if .NET framework 4 is used. My 5! to your solution
Try
VB
If (Convert.ToString(strr) = "") Then
       Response.Write("HAI")
End If


The difference between Convert.ToString(strr) and strr.ToString() is that the latter throws an error if the string is null.
 
Share this answer
 
Comments
Ed Nutting 3-Mar-12 5:55am    
Errr....Yeah may work but massively pointless and comparatively inefficient - Convert methods take aaggess to run in my experience - better that the OP learns to use built in features properly. My 4.
Sergey Alexandrovich Kryukov 3-Mar-12 20:50pm    
Should be '==', not '='.
[EDIT: Opps..! failed to see it's VB.NET]
--SA
Abhinav S 4-Mar-12 3:26am    
VB.Net code, so = is fine right?
Sergey Alexandrovich Kryukov 4-Mar-12 3:44am    
Sorry, please ignore it. I failed to notice it's VB.NET.
--SA
if(str=="")
{
response.write("ur msg");
}
 
Share this answer
 
Comments
Ed Nutting 3-Mar-12 5:56am    
This doesn't solve the problem and is basically what the OP had already just without the error. Furthermore you have used no formatting nor given any additional help to what had already been posted. My 1.
Sergey Alexandrovich Kryukov 4-Mar-12 3:46am    
Agree.
--SA
Dave Kreskowiak 3-Mar-12 13:19pm    
Yeah, that's not going to work if str is null, which is one of the conditions the OP was looking to check for.

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