构建枚举标志列表
Building a list of enum flags
我有这个方法接受多个 JobSetting
标志
private void ProcessTheData(string[] dataFiles, PictureBox sender, JobSetting? jobSetting)
Flags 枚举是这个
[Flags]
enum JobSetting
{
ForcePrint = 1,
ForceProof = 2,
UseDefault = 4,
SkipImposition = 8,
HighPriority = 16,
SetDP = 32
}
我有一个查看 CheckboxList 的事件处理程序,我根据选中的选项构建了一个标志列表。
JobSetting js;
if (((Control)sender).Name.ToLower().Contains("proof"))
{
if (chkbxList_Proof.CheckedItems.Contains("Skip Imposition") && chkbxList_Proof.CheckedItems.Contains("High Priority") && chkbxList_Proof.CheckedItems.Contains("_SetDP"))
{
js = (JobSetting.ForceProof | JobSetting.SkipImposition | JobSetting.HighPriority | JobSetting.SetDP);
}
else if (chkbxList_Proof.CheckedItems.Contains("Skip Imposition") && chkbxList_Proof.CheckedItems.Contains("High Priority"))
{
js = (JobSetting.ForceProof | JobSetting.SkipImposition | JobSetting.HighPriority);
}
else if (chkbxList_Proof.CheckedItems.Contains("Skip Imposition"))
{
js = (JobSetting.ForceProof | JobSetting.SkipImposition);
}
else if (chkbxList_Proof.CheckedItems.Contains("High Priority"))
{
js = (JobSetting.ForceProof | JobSetting.HighPriority);
}
else if (chkbxList_Proof.CheckedItems.Contains("_SetDP"))
{
js = (JobSetting.ForceProof | JobSetting.SetDP);
}
else
{
js = JobSetting.ForceProof;
}
}
一切正常...但我觉得这是构建我的标志列表的最不有效的方法。
我想以这种方式构建它,或者任何已经存在的正确方法。
JobSeetings js = JobSetting.ForceProof;
if(chkbxList_Proof.CheckedItems.Contains("Skip Imposition"))
{
js.add(JobSetting.SkipImposition)
}
if(chkbxList_Proof.CheckedItems.Contains("High Priority"))
{
js.add(JobSetting.HighPriority)
}
您可以根据选中的项目将标志与按位或结合起来。
JobSetting js = JobSetting.ForceProof;
if (chkbxList_Proof.CheckedItems.Contains("Skip Imposition"))
{
js |= JobSetting.SkipImposition;
}
if (chkbxList_Proof.CheckedItems.Contains("High Priority"))
{
js |= JobSetting.HighPriority;
}
//etc, etc...
如果选中"Skip Imposition"
和"High Priority"
,则js
将等于JobSetting.ForceProof | JobSetting.SkipImposition | JobSetting.HighPriority
。
我有这个方法接受多个 JobSetting
标志
private void ProcessTheData(string[] dataFiles, PictureBox sender, JobSetting? jobSetting)
Flags 枚举是这个
[Flags]
enum JobSetting
{
ForcePrint = 1,
ForceProof = 2,
UseDefault = 4,
SkipImposition = 8,
HighPriority = 16,
SetDP = 32
}
我有一个查看 CheckboxList 的事件处理程序,我根据选中的选项构建了一个标志列表。
JobSetting js;
if (((Control)sender).Name.ToLower().Contains("proof"))
{
if (chkbxList_Proof.CheckedItems.Contains("Skip Imposition") && chkbxList_Proof.CheckedItems.Contains("High Priority") && chkbxList_Proof.CheckedItems.Contains("_SetDP"))
{
js = (JobSetting.ForceProof | JobSetting.SkipImposition | JobSetting.HighPriority | JobSetting.SetDP);
}
else if (chkbxList_Proof.CheckedItems.Contains("Skip Imposition") && chkbxList_Proof.CheckedItems.Contains("High Priority"))
{
js = (JobSetting.ForceProof | JobSetting.SkipImposition | JobSetting.HighPriority);
}
else if (chkbxList_Proof.CheckedItems.Contains("Skip Imposition"))
{
js = (JobSetting.ForceProof | JobSetting.SkipImposition);
}
else if (chkbxList_Proof.CheckedItems.Contains("High Priority"))
{
js = (JobSetting.ForceProof | JobSetting.HighPriority);
}
else if (chkbxList_Proof.CheckedItems.Contains("_SetDP"))
{
js = (JobSetting.ForceProof | JobSetting.SetDP);
}
else
{
js = JobSetting.ForceProof;
}
}
一切正常...但我觉得这是构建我的标志列表的最不有效的方法。
我想以这种方式构建它,或者任何已经存在的正确方法。
JobSeetings js = JobSetting.ForceProof;
if(chkbxList_Proof.CheckedItems.Contains("Skip Imposition"))
{
js.add(JobSetting.SkipImposition)
}
if(chkbxList_Proof.CheckedItems.Contains("High Priority"))
{
js.add(JobSetting.HighPriority)
}
您可以根据选中的项目将标志与按位或结合起来。
JobSetting js = JobSetting.ForceProof;
if (chkbxList_Proof.CheckedItems.Contains("Skip Imposition"))
{
js |= JobSetting.SkipImposition;
}
if (chkbxList_Proof.CheckedItems.Contains("High Priority"))
{
js |= JobSetting.HighPriority;
}
//etc, etc...
如果选中"Skip Imposition"
和"High Priority"
,则js
将等于JobSetting.ForceProof | JobSetting.SkipImposition | JobSetting.HighPriority
。