Test if a string is empty in c#

Gonna sound like a really stupid question but I just dont get how to test if string contains any data. In this case I'm casting it from session. Documentation I've read states one should test by the length of string as per below. However when I do this I just get the old "object not set to an instance of an object" error referring to the string.

public class Index : System.Web.UI.Page
{
      private string sString;

      private void Page_Load(object sender, System.EventArgs e)
      {
            sString = (string) Session["something"];

            if(sString.length > 0)
            {
            }

      }
}

What is the best way to initiate and then test strings?
jonnyboy69Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
NTACConnect With a Mentor Commented:
Well you first want to check to see if the reference is null--that way you won't get your exception that you are getting.

So do the check

if( sString != null )
{  //then the code you want to do here
   if(sString.Length > 0 )
   {
     //rest here
    }
}  

Good Luck
NTAC
0
 
NTACCommented:
In the example you show above--you just have a reference to a string--sString.  you do not instantiate it, so you are going to get that error every time.

If this is purely for testing, you should instantiate it first.  

in your constructor you could set:  
sString = "Hi I'm a string";

0
 
tinchosCommented:
Just to comment on NTAC last comment

but as far as I know, you do not need to instantiate it.

as you're assining a value to it

>>         sString = (string) Session["something"];

you don't need to instantiate it again....

what would be the purpose of

sString = "Hi I'm a string";
sString = (string) Session["something"];

What must be told is that you won't be able to use the string until the assignment operation to it has been run, otherwise it will have a null value.

Tincho

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jonnyboy69Author Commented:
OK but that doesnt actually work using the length property which is what you are meant to use. e.g.

sString = (string) Session["something"];

if(sString.length > 0)
{
}

If the session object is null I get an error on the length test. I would have thought the sString var would have been instantiated by this (even if the session object is empty), however it isnt set to string.Empty or string == "" lke you would think.

I realise I can probably go like this, it just doesnt seem right to me?

if(Session["something"] != null)
  sString = (string) Session["something"];
else
 sString = "";

then I can go if(string.length == 0) etc....
0
 
jonnyboy69Author Commented:
and.... why doesnt this work?

sReferrer = "";
sReferrer = (string) Session["Referrer"];
if(sReferrer.Length > 0)
{
      sReferrer = Request.UrlReferrer.ToString();
      Session["Referrer"] = sReferrer;
}

sReferrer is still null when its length is tested?
0
 
tinchosCommented:
If the session object is null, then your sReferrer field will always be null (as Session["whatever"] will be null.

if you use

if(Session["something"] != null)
  sString = (string) Session["something"];
else
 sString = "";

if your session object is equal to null, then the length of sstring will be 0.

If not, it will hasve the length of the session["something] value.

Tincho
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.

All Courses

From novice to tech pro — start learning today.