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

  • SEARCH
  • Home
  • 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 8407403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:21:18+00:00 2026-06-09T23:21:18+00:00

I have a number of methods where I perform validation – tryParse being one

  • 0

I have a number of methods where I perform validation – tryParse being one of the main functions i use.

I end up with code like this:

bool isValid = true;

int dealId;
isValid = !int.TryParse(strArr[0], out dealId) ? false : isValid;

DateTime createdOn;
isValid = !DateTime.TryParse(strArr[1], out createdOn) ? false : isValid;

isValid = !tmp.Add(new BookmarkedDeal(userId, dealId, createdOn)) ? false : isValid;

Is there a better way of setting isValid to false only if the result is false?

  • 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-09T23:21:20+00:00Added an answer on June 9, 2026 at 11:21 pm

    This depends on what you definition of “better” is, but you can do something like this:

    int dealId;
    DateTime createdOn;
    
    return int.TryParse(strArr[0], out dealId) &&
           DateTime.TryParse(strArr[1], out createdOn) && 
           tmp.Add(new BookmarkedDeal(userId, dealId, createdOn))
    

    (assuming of course you are returning this from a method, otherwise, you can just set the value of this to a bool isValid and use it as you did before)

    && is called the conditional-AND operator. The basic idea is it will only evaluate far enough to see if the returned value will satisfy the condition(s). So first it will check to see if int.TryParse() returns a true, if it does, it will check DateTime.TryParse() and then the following method if it returns true. The beauty of this operator is if any fails, then it knows that it cannot possibly be true and returns a false immediately.

    (There is also a conditional-OR operator (||) that acts the same way, but if evaluates only fair enough until it hits a true, hence making the entire expression true).

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

Sidebar

Related Questions

I have a number of existing methods within a controller. Each one of those
In a Rails 3.2 app I have defined two methods that perform a calculation
I have a -(void) method in my main UITableView class, which I use to
Using Rails 3 I have a number of model containing serialized attributes. To perform
So I have a really long chain of methods, something similar to: return self.append_command(fbghasjfa).append_command(input_file_part).append_command(output_video_codec_part).append_command(output_resolution_part).append_command(output_video_bitrate_part).append_command(strict_part).append_command(output_audio_codec_part).append_command(output_number_of_audio_channels_part).append_command(output_audio_bitrate_part).append_command(output_file_part).__finalized
I have a method: public void StoreNumberInSmallestType(ValueType number) { if (numberTypes == null) numberTypes
I have and old(ish) C# method I wrote that takes a number and converts
I have a method connection(int n) which gives me all the cells number that
Say I want to have a method that takes any kind of number, is
I have number of line breaks in a string. But I only want it

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.