Solved

Test if a string is empty in c#

Posted on 2004-08-06
6
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 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