Designer Limitations

I was wondering if there is a way to limit the value in the construct. Here is my code:

class Student : Human 
{
    private double Grade;

    public Student(string FirstName, string LastName, double Grade)
        : base(FirstName, LastName)
    {
        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Grade = Grade;
    }
}

and when I create a new Student, I want to limit the class between> = 2.00 and <= 6.00, for example, compilation or exception at runtime. Is there any way? (Don’t worry about other FirstName and LastName fields)

+5
source share
7 answers

You can check this and throw an exception at runtime, for example:

if (grade < 2.00 || grade > 6.00)
    throw new ArgumentOutOfRangeException("grade");

Always put such conditions at the beginning of a method or constructor. I even put them on my own #region(but this is my personal preference):

public Student(string firstName, string lastName, double grade)
    : base(firstName, lastName)
{
    #region Contract
    if (grade < 2.00 || grade > 6.00)
        throw new ArgumentOutOfRangeException("grade");
    #endregion
    this.FirstName = firstName;
    this.LastName = lastName;
    this.Grade = grade;
}

, , Code Contracts. MSDN, . - Visual Studio Microsoft. , . :

using System.Diagnotistics.Contracts;

public Student(string firstName, string lastName, double grade)
    : base(firstName, lastName)
{
    #region Contract
    Contract.Requires<ArgumentOutOfRangeException>(grade >= 2.00);
    Contract.Requires<ArgumentOutOfRangeException>(grade <= 6.00);
    #endregion
    this.FirstName = firstName;
    this.LastName = lastName;
    this.Grade = grade;
}
+12

:

class Student : Human 
{
    private double Grade;

    public Student(string FirstName, string LastName, double Grade)
        : base(FirstName, LastName)
    {
        if (Grade >= 2 && Grade <= 6) { 
          throw new ArgumentOutOfRangeException();
        }

        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Grade = Grade;
    }
}

Microsoft Code, :

class Student : Human  {
    private double Grade;

    public Student(string FirstName, string LastName, double Grade)
        : base(FirstName, LastName)
    {
        System.Diagnotistics.Contracts.Contract.Requires<ArgumentOutOfRangeException>(Grade >= 2 && Grade <= 6);

        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Grade = Grade;
    }
}
+4

, , , double, Grade. Grade double . , Grade, Student Grade .

public struct Grade
{
    public static readonly double MinValue = 2.0;
    public static readonly double MaxValue = 6.0;

    private double value;

    public static explicit operator double(Grade grade)
    {
        return grade.value + MinValue;
    }

    public static explicit operator Grade(double gradeValue)
    {
        if (gradeValue < MinValue || gradeValue > MaxValue)
            throw new ArgumentOutOfRangeException("gradeValue", "Grade must be between 2.0 and 6.0");

        return new Grade{ value = gradeValue - MinValue };
    }
}

:

double userInput = GetUserInputForGrade();
Grade grade = (Grade)userInput; // perform explicit cast.
Student student = new Student(firstName, lastName, grade);

: @EricLippert, min/max .

2: @JeppeStigNielsen. value MinValue, default(Grade) ( MinValue) , 0 .

+1
class Student : Human 
{
    private double Grade;

    public Student(string FirstName, string LastName, double Grade)
        : base(FirstName, LastName)
    {
        if (Grade < 2 || Grade > 6)
            throw new ArgumentOutOfRangeException("Grade must be between 2 and 6");

        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Grade = Grade;
    }
}
0
public Student(string FirstName, string LastName, double Grade)
    : base(FirstName, LastName)
    {
     if(Grade >= 2.0 || Grade <= 6.00)
      throw  new ArgumentException("your message");     
    }
0

, , . , , , .

public Student(string firstName, string lastName, decimal grade)
{
    Contract.Requires(grade >= 2);
    Contract.Requires(grade <= 6);

    FirstName = firstName;
    LastName = lastName;
    Grade = grade;     
}

0
class Student : Human 
{
    private double Grade;

    public Student(string FirstName, string LastName, double Grade)
        : base(FirstName, LastName)
    {
        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Grade = Grade;
        if(Grade >= 2 and Grade <= 6){
            throw new Exception("Incorrect grade input");
    }
}

, , . ArgumentOutOfException ..

-2

All Articles