Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Test if a string is empty in c#

Posted on 2004-08-06
6
Medium Priority
?
303 Views
Last Modified: 2008-03-10
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?
0
Comment
Question by:jonnyboy69
  • 2
  • 2
  • 2
6 Comments
 
LVL 4

Accepted Solution

by:
NTAC earned 150 total points
ID: 11735464
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
 
LVL 4

Expert Comment

by:NTAC
ID: 11735489
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
 
LVL 9

Expert Comment

by:tinchos
ID: 11735599
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:jonnyboy69
ID: 11735684
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
 

Author Comment

by:jonnyboy69
ID: 11735752
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
 
LVL 9

Expert Comment

by:tinchos
ID: 11735814
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

876 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