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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:57:04+00:00 2026-05-23T16:57:04+00:00

I have a class with many fields which represents different physical values. class Tunnel

  • 0

I have a class with many fields which represents different physical values.

class Tunnel
{
    private double _length;
    private double _crossSectionArea;
    private double _airDensity;
    //...

Each field is exposed using read/write property. I need to check on setter that the value is correct and generate exception otherwise. All validations are similar:

    public double Length
    {
        get { return _length; }
        set
        {
            if (value <= 0) throw new ArgumentOutOfRangeException("value",
                    "Length must be positive value.");
            _length = value;
        }
    }

    public double CrossSectionArea
    {
        get { return _crossSectionArea; }
        set
        {
            if (value <= 0) throw new ArgumentOutOfRangeException("value",
                    "Cross-section area must be positive value.");
            _crossSectionArea = value;
        }
    }

    public double AirDensity
    {
        get { return _airDensity; }
        set
        {
            if (value < 0) throw new ArgumentOutOfRangeException("value",
                    "Air density can't be negative value.");
            _airDensity = value;
        }
    }
    //...

Is there any elegant and flexible way to accomplish such validation?

  • 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-23T16:57:04+00:00Added an answer on May 23, 2026 at 4:57 pm

    Assuming you want this sort of behaviour, you might consider some helper methods, e.g.

    public static double ValidatePositive(double input, string name)
    {
        if (input <= 0)
        {
            throw new ArgumentOutOfRangeException(name + " must be positive");
        }
        return input;
    }
    
    public static double ValidateNonNegative(double input, string name)
    {
        if (input < 0)
        {
            throw new ArgumentOutOfRangeException(name + " must not be negative");
        }
        return input;
    }
    

    Then you can write:

    public double AirDensity
    {
        get { return _airDensity; }
        set
        {            
            _airDensity = ValidationHelpers.ValidateNonNegative(value,
                                                                "Air density");
        }
    }
    

    If you need this for various types, you could even make it generic:

    public static T ValidateNonNegative(T input, string name)
        where T : IComparable<T>
    {
        if (input.CompareTo(default(T)) < 0)
        {
            throw new ArgumentOutOfRangeException(name + " must not be negative");
        }
        return input;
    }
    

    Note that none of this is terribly i18n-friendly…

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

Sidebar

Related Questions

I have a class which has many small functions. By small functions, I mean
I have the following model class Plugin(models.Model): name = models.CharField(max_length=50) # more fields which
Let's say you have a class called Customer, which contains the following fields: UserName
I have a class which contains a number of fields, and all are annotated
I have a model class 'Market' which has many products: class Market < ActiveRecord::Base
I have a pretty big data entry form which has many text fields ,
I have a class that has a number of fields, two of which are
I have a method which accesses many fields, so it’s hard coded into its
I'm developing an application that will have a parent class which will have many
I have a class with many embedded assets. Within the class, I would like

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.