Solved

Test if a string is empty in c#

Posted on 2004-08-06
6
299 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 50 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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