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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:34:12+00:00 2026-06-07T19:34:12+00:00

So, to start, I have seen posts like this: How to find which condition

  • 0

So, to start, I have seen posts like this: How to find which condition is true without using if statement

It’s not quite what I need, although the idea is pertinent, in that I would like it to be more readable code.

I think Switch is the best bet, but let me explain.

I have this statement:

if (input == string.Empty || typeComboBox.Text == null)
{
    MessageBox.Show("Nothing to encrypt!", "Nothing Selected!");            
    return null;
}

So the idea here is that I used to have this statement broken into two “IF” statements, which isn’t a huge deal, but for readability sake, and my on going effort of reducing code, I wanted to combine the statements into one.

If input is empty, I want the first argument in MessageBox.Show to appear, but not the second.

If typeComboBox.Text is null, I want the second option to appear, but not the first.

If they are both true statements, I want both to appear.

Now, my goal was to have these both done without the use of more than one test or method. Basically, I mean this: if I can find which condition is true and have the resultant data output within the same statement, that would be ideal.

I see switches being an option, and I don’t understand them very well yet, but I think that would require me to make a decision method based on the outcome of this test, and send that outcome to the switch; which wouldn’t be ideal, as I could simply have two if statements and less code.

Is there any way to do this in one statement? It’s not necessary for this specific program, but I want to know for the future.

Thanks!

  • 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-06-07T19:34:13+00:00Added an answer on June 7, 2026 at 7:34 pm

    I am assuming that you started with this code:

    if (input == string.Empty)
    {
        MessageBox.Show("Nothing to encrypt!");            
        return null;
    }
    
    if (typeComboBox.Text == null)
    {
        MessageBox.Show("Nothing Selected!");            
        return null;
    }
    

    I don’t consider there to be anything wrong with this code at all, and this is probably the most readable. It will perform exactly as many tests as necessary, and no more. Any alternative will result in more tests being performed, even though you may wind up with less code. For example:

    if (input == string.Empty || typeComboBox.Text == null)
    {
        MessageBox.Show((input == string.Empty) ? "Nothing to encrypt!" : "Nothing Selected!");
        return null;
    }
    

    Less lines of code, but in a failure scenario there will be two or three tests performed instead of one or two. It’s also a bit less straightforward.

    Terse code is nice, but make it too terse and it becomes harder to maintain. Readability lies somewhere between verbose and terse, and in this case the more verbose code is more readable, in my opinion.


    Another option is to consider the fact that it would be appropriate to report multiple errors. For that, try code like this:

    List<string> errors = new List<string>();
    
    if (input == string.Empty)
    {
        errors.Add("Nothing to encrypt.");
    }
    
    if (typeComboBox.Text == null)
    {
        errors.Add("Nothing selected.");
    }
    
    if (errors.Count != 0)
    {
        MessageBox.Show(string.Join(" ", errors.ToArray()));
        return null;
    }
    

    This is a bit more verbose than your original code, but it will allow all relevant errors to be reported instead of only the first one encountered.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen alot of posts about it stopping sound early. This is not
I've seen other posts on this subject and have fiddled with variations but still
Hi all I have seen all question related to this problem but couldn't find
I have seen many posts about using the clock() function to determine the amount
I have seen other user posts which show Stopwatch measuring time spent in Thread.Sleep(5000)
I have start date and end date. I need to find out the day
I have seen some posts of the asp:menu and the browsers Chrome and Safari
Hi all I have seen many articles on url rewriting but I didn't find
I've seen a million different posts and (as odds would have it) a million
I have seen a few post on this, but nothing that would work entirely

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.