编辑:我已经改变了问题的标题,包括验证的参数,要更清楚我所问。使用整数和验证参数设置枚举属性
我试图在C#中创建一个类,它有一个属性设置一个int并返回一个枚举,这可能是非常基本的,但我是一个C#noob。
通过使用int参数进行的设置是一个特殊的限制,我不会介绍它,它是通过Vbscript在COM上设置的。
我期待创建这些RSSCalendarDay对象,以便它们可以放在列表中,并且每个对象都有一个从周日到周六(1到7)的唯一RSSCalendarRuleDay。
它是使用xCal和本地数据进行重复性事件的真正基本方法的一部分。
到目前为止我的代码是下面,你可以看到得到&集是不完整的,我被困在变元的验证(检查它的一个int第一,等等),这就是我以前试图用dayOrderValue为,所有我可以在网上找到关系是使用int.Parse和int.TryParse,但它们是用于解析一个字符串,而不是我想要做的。
public class RSSCalendarDay {
private RSSCalendarRuleDay? _dayOrder;
/// <summary>
/// Passed an Integer from 1 to 7 to represent the particular day of the week as an enum
/// </summary>
public RSSCalendarRuleDay? DayOrder {
get {
// TODO
}
set {
if (value != null) {
int dayOrderValue;
if() {
throw new ArgumentException("Invalid day order specified");
}
if(dayOrderValue < 1 || dayOrderValue > 7) {
throw new ArgumentException("Invalid day type specified (out of day range of 1 to 7)");
}
_dayOrder = (RSSCalendarRuleDay) dayOrderValue;
}
else {
_dayOrder = null;
}
}
}
public DateTime? TimeOpens { get; set; }
public DateTime? TimeCloses { get; set; }
/// <summary>
/// These are the relevant BYDAY abbreviations used in the xCal recurring rule specification
/// to represent what days a recurring event is open in a comma separated list
/// </summary>
public enum RSSCalendarRuleDay {
SU=1,MO=2,TU=3,WE=4,TH=5,FR=6,SA=7
}
}
好的回答:)我觉得这个“价值!= 0”也很重要,但很微妙,我会用你的建议来代替。谢谢 – Pricey 2012-02-07 20:38:59
而是,我的意思是“而不是我现有的代码”。 – Pricey 2012-02-07 20:53:04
我现在认为第二个片段可能更接近你所需要的。第一个片段仅仅将'null'替换为'0'。 – Douglas 2012-02-07 21:12:33