Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A class named student has the following attribute private numeric age write the

ID: 3634964 • Letter: A

Question

A class named student has the following attribute
private numeric age
write the get /set methods for the above attribute.

Explanation / Answer

public class NumericLessThanAttribute : ValidationAttribute, IClientValidatable 02 { 03 private const string lessThanErrorMessage = "{0} must be less than {1}."; 04 private const string lessThanOrEqualToErrorMessage = "{0} must be less than or equal to {1}."; 05 06 public string OtherProperty { get; private set; } 07 08 private bool allowEquality; 09 10 public bool AllowEquality 11 { 12 get { return this.allowEquality; } 13 set 14 { 15 this.allowEquality = value; 16 17 // Set the error message based on whether or not 18 // equality is allowed 19 this.ErrorMessage = (value ? lessThanOrEqualToErrorMessage : lessThanErrorMessage); 20 } 21 } 22 23 public NumericLessThanAttribute(string otherProperty) 24 : base(lessThanErrorMessage) 25 { 26 if (otherProperty == null) { throw new ArgumentNullException("otherProperty"); } 27 this.OtherProperty = otherProperty; 28 } 29 30 public override string FormatErrorMessage(string name) 31 { 32 return String.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, this.OtherProperty); 33 } 34 35 protected override ValidationResult IsValid(object value, ValidationContext validationContext) 36 { 37 PropertyInfo otherPropertyInfo = validationContext.ObjectType.GetProperty(OtherProperty); 38 39 if (otherPropertyInfo == null) 40 { 41 return new ValidationResult(String.Format(CultureInfo.CurrentCulture, "Could not find a property named {0}.", OtherProperty)); 42 } 43 44 object otherPropertyValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null); 45 46 decimal decValue; 47 decimal decOtherPropertyValue; 48 49 // Check to ensure the validating property is numeric 50 if (!decimal.TryParse(value.ToString(), out decValue)) 51 { 52 return new ValidationResult(String.Format(CultureInfo.CurrentCulture, "{0} is not a numeric value.", validationContext.DisplayName)); 53 } 54 55 // Check to ensure the other property is numeric 56 if (!decimal.TryParse(otherPropertyValue.ToString(), out decOtherPropertyValue)) 57 { 58 return new ValidationResult(String.Format(CultureInfo.CurrentCulture, "{0} is not a numeric value.", OtherProperty)); 59 } 60 61 // Check for equality 62 if (AllowEquality && decValue == decOtherPropertyValue) 63 { 64 return null; 65 } 66 // Check to see if the value is greater than the other property value 67 else if (decValue > decOtherPropertyValue) 68 { 69 return new ValidationResult(FormatErrorMessage(validationContext.DisplayName)); 70 } 71 72 return null; 73 } 74 75 public static string FormatPropertyForClientValidation(string property) 76 { 77 if (property == null) 78 { 79 throw new ArgumentException("Value cannot be null or empty.", "property"); 80 } 81 return "*." + property; 82 } 83 84 public IEnumerable GetClientValidationRules(ModelMetadata metadata, ControllerContext context) 85 { 86 yield return new ModelClientValidationNumericLessThanRule(FormatErrorMessage(metadata.DisplayName), FormatPropertyForClientValidation(this.OtherProperty), this.AllowEquality); 87 } 88 }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote