0

I am able to assign a variable like below:

if (Session["myVariable"] != null)
{
    string variAble = Session["myVariable"].ToString();
}

Is there a method which checks whether an object is null or not and then assign if it is not null?

7
  • 1
    string.IsNullOrEmpty() Commented Jan 17, 2013 at 22:29
  • ye but it does not handle other objects such as session. if the object you are comparing is string it works. Commented Jan 17, 2013 at 22:30
  • Why this method does not suitable for you? Commented Jan 17, 2013 at 22:31
  • so if the value is null you dont want to assign the variable at all, or provide a default value? could you write some puedo code on how you want this to work Commented Jan 17, 2013 at 22:32
  • @sa_ddam213 if you don't make that null check comparison your application would throw error if that session is null. so i am looking for a method that would do session null check and if not null assign to string like string.IsNullOrEmpty Commented Jan 17, 2013 at 22:36

3 Answers 3

5

string variAble = Session["myVariable"] ?? "";

EDIT A slightly more robust form, as suggested by @hatchet, is:

string variAble = (Session["myVariable"] ?? "").ToString();

Sign up to request clarification or add additional context in comments.

4 Comments

If he needed to do the ToString, then = (Session["myVariable"] ?? "").ToString();
@ReedCopsey - that can be handled with the slight modification in my comment above.
@ReedCopsey I thought if Session[] returns an object (or boxed value type), then that would work since all object descendants have ToString.
@hatchet but it returns it as a string, not an int, etc. That was what I was trying to say.
2

While this isn't anything new, you can use the conditional operator to potentially simplify this:

string variable = Session["myVariable"] != null ? Session["myVariable"].ToString() : "Fallback";

1 Comment

@HamletHakobyan Nope - always been there.
2

You could write an extension method, as those still work with null objects.

public static class StringExtensions
{
    public static String ToNullString(this object o)
    {
        return o == null ? "" : o.ToString();
    }
}

I would consider it poor form though - it'll be confusing to whoever will be supporting the code after you, or even to you a few months down the track. It's probably better to just do the null check.

10 Comments

but i suppose this would not be entire application wide am i incorrect ? can i define an extension method for entire asp.net website ?
@TheEvilPenguin, StringExtensions will have to be a static class
@MonsterMMORPG As long as you include the namespace the class is in, you'll be able to use it anywhere that references the assembly it's in
@sa_ddam213 Thanks. That's what I get for not compiling code before I post it
@TheEvilPenguin instead of me to go each existing page and add reference can i add it globally somehow ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.