Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6696119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:18:12+00:00 2026-05-26T06:18:12+00:00

I have an object with string properties, for example, a Patient object with weight

  • 0

I have an object with string properties, for example, a Patient object with weight and height.

But when the property is null, my code fails when I try to use it, as the property is set to null. I’m trying to create a function that checks if the string/property is null, and if so, set the property to "".

I know that I can do this:

if(string.isNullOrEmpty(patient.weight)) patient.weight = "";

But I need the code to be as clean as possible, and I have a lot of properties, so I don’t want to manually check each one. Ideally, I’d like to have a function that can take the string, (without failing even if it is null), and simply return either the value if it’s not null, or a "" if it is null.

Can anyone give me a clue on this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:18:12+00:00Added an answer on May 26, 2026 at 6:18 am

    Personally I would ensure those properties can never be null by writing them like this:

    private string _Name = string.Empty;
    public string Name
    {
        get
        {
            return _Name;
        }
        set
        {
            _Name = value ?? string.Empty;
        }
    }
    

    However, what you’re looking for is probably the ?? operator, also known as the null-coalescing operator, as used above, basically, this expression:

    x = y ?? z;
    

    means the same as this:

    if (y != null)
        x = y;
    else
        x = z;
    

    and that’s not entirely true either. In the above example y is evaluated twice, which does not happen with the ?? operator so a better approximation would be this:

    var temp = y;
    if (temp != null)
        x = temp;
    else
        x = z;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java Properties object that I load from an in-memory String ,
I have an object with a string[] property. When I assign this as the
I have a C# string object that contains the code of a generic method,
I have persistent object, with a string property that often is over 500 charachters.
I have a string object with multiple characters and even special characters I am
I have a String Object in format yyyyMMdd .Is there a simple way to
I have a ComboBox with a label function like this one: private function fieldLabelFunction(item:Object):String
I'm an absolute beginner, you see. Say I have a string object on the
I have this object: class a { public string Application; public DateTime From, To;
I have an object that can build itself from an XML string, and write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.