public static Tenor Parse(string value) { Tenor result = new Tenor(); if (value.Length >= 2) { switch (value[0]) { case 'D': result.TenorType = TenorTypeValue.Day; break; case 'W': result.TenorType = TenorTypeValue.Week; break; case 'M': result.TenorType = TenorTypeValue.Month; break; case 'Y': result.TenorType = TenorTypeValue.Year; break; } string number = value.Substring(1); try { result.Offset = Convert.ToInt32(number); if (result.TenorType != TenorTypeValue.Invalid) { return(result); } } catch (FormatException ex) { throw ThrowHelper.New <ArgumentException>(ExceptionContext, ex, ErrorMessages.InvalidTenorValue, value); } } throw ThrowHelper.New <ArgumentException>(ExceptionContext, ErrorMessages.InvalidTenorValue, value); }
/// <summary> /// Compares the current instance with another object of the same type and returns an integer that indicates /// whether the current instance precedes, follows, or occurs in the same position in the sort order as the /// other object. /// </summary> /// <param name="obj">An object to compare with this instance.</param> /// <returns>A value that indicates the relative order of the objects being compared. The return value has these meanings: /// <list type="bullet"> /// <item><description>Less than zero - this instance precedes obj in the sort order.</description></item></list> /// <item><description></description>Zero - this instance occurs in the same position in the sort order as obj.</item></list> /// <item><description>Greater than zero - this instance follows obj in the sort order.</description></item></list> /// </returns> public int CompareTo(object obj) { // Null references are by definition less than the current instance. if (obj == null) { return(1); } if (!(obj is Tenor)) { throw ThrowHelper.New <ArgumentException>(this, InternalErrors.UnexpectedArgumentType, obj.GetType().FullName, this.GetType().FullName); } Tenor rhs = (Tenor)obj; if (rhs == this) { return(0); } return(rhs <= this ? 1 : -1); }