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 6954665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:38:05+00:00 2026-05-27T14:38:05+00:00

I have multiple variables which I set prior to creating an object, I want

  • 0

I have multiple variables which I set prior to creating an object, I want to check if any of those variables in null, if any variable then display an error. Is there a way to incorporate this in a foreach loop?

For eg.

Var Var1 = blah1;
Var Var2 = blah2;
Var Var3 = blah3;
Var Var4 = blah4;
Var Var5 = blah5;

foreach(var above, if any is null)
Errmessage

Thanks in advance

  • 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-27T14:38:06+00:00Added an answer on May 27, 2026 at 2:38 pm

    I, personally, would have separate checks for each variable. On “error message” for multiple validation checks is a bad idea.

    The main reason for this is that your “error message” should likely be an ArgumentNullException, which should provide the proper parameter name. This will be different per variable. Even if you use a custom exception, providing information about which variable was improperly specified is worth the extra coding effort.

    That being said, if you want to do this, you can use:

    var Var1 = blah1;
    var Var2 = blah2;
    var Var3 = blah3;
    var Var4 = blah4;
    var Var5 = blah5;
    
    if ( (new object[] {Var1, Var2, Var3, Var4, Var5}).Any(v => v==null))
        throw new Exception("Your error here");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a variable obj: Option[MyObject] and want to extract multiple variables from it
I have text documents like the following which contain single and multiple variables: title::
I have a int variable which hold multiple flags, for instance: int styles =
I have a variable in XSLT called variable_name which I am trying to set
I had a question about Java Class Path variables. If I have multiple jars
The situation: I have multiple classes that should each hold a variable with a
I have a multiple language website, and I use a php get variable to
We have multiple MFC apps, which use CMutex( false, blah ), where blah allows
I'd like to distribute a set of build variables (which someone should append to
I need to have multiple submit buttons. I have a form which creates an

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.