迭代按位枚举的保存值

发布于 2024-09-25 09:54:47 字数 673 浏览 4 评论 0原文

我使用按位枚举作为中继器控件的数据源,其中每行都有一个复选框。当用户保存数据时,与复选框相对应的枚举值将保存到单个数据库字段中。

但是,当用户编辑数据时,显然我需要用现有值预先填充中继器。我可以通过交互枚举的可能值并相应地选中复选框,以简单的值来完成此操作。但是,理想情况下,我真的希望我的代码保持独立于数据的更改:因此,如果我更改或添加枚举中的值,我的代码仍然可以工作,而无需更改我控件中的代码。

似乎要做的显而易见的事情是在我从数据库中获取枚举字段中的现有值后尝试迭代它。换句话说,如果保存的值对应于枚举中的几个选择中的两个,我需要循环遍历这两个。但是我看不到可以用来执行此操作的方法或属性。有人能指出我正确的方向吗?

一个示例...可能有助于理解这一点...是我可以通过将其转换为字符串并拆分它来获得我想要的效果,但它很笨拙(comsPrefs 是有问题的枚举)

        Dim selectedPrefs As String = comsPrefs.ToString()
        Dim splitStr() As String = selectedPrefs.Split(","c)
        Dim i As Integer

        For i = 0 To splitStr.Length - 1
            'do some cool stuff
        Next

I'm using a Bitwise Enum as a Datasource for a repeater control in which each line has a checkbox. When the user saves data, enum values corresponding to the checked boxes are saved into a single database field.

However, when the user comes to edit the data, obviously I need to pre-populate the repeater with the existing values. I can do this in a straightforward value by interating through the possible values of the enum and checking boxes accordingly. However, ideally I'd really like my code to remain independent of changes to the data: so if I change the or add to the values in the Enum, my code will still work without changing the code in my control.

It seems the obvious thing to do is try to iterate through the existing values in the enum field once I've got it back from the database. In other words, if the saved value corresponds to two selections out of the several in the enum, I need to loop through just those two. However I can see no method or property that I can use to do this. Can anyone point me in the right direction?

A sample ... which might help make sense of this ... is that I can get sort of the effect I want by casting it to a string and splitting it, but it's clumsy (comsPrefs is the enum in question)

        Dim selectedPrefs As String = comsPrefs.ToString()
        Dim splitStr() As String = selectedPrefs.Split(","c)
        Dim i As Integer

        For i = 0 To splitStr.Length - 1
            'do some cool stuff
        Next

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

靑春怀旧 2024-10-02 09:54:48

像这样的东西吗?

[Flags]
public enum YourEnum
{
    One = 1, Two = 2, Four = 4, Eight = 8
}

// ...

YourEnum exampleData = YourEnum.Two | YourEnum.Four;

var values = Enum.GetValues(typeof(YourEnum))
                 .Cast<YourEnum>()
                 .Where(x => exampleData.HasFlag(x));

foreach (var v in values)
{
    Console.WriteLine(v);
}

请注意,HasFlag 方法仅在 .NET 4 中可用 - 如果您使用的是早期版本的框架,那么您需要使用 Where(x => (exampleData & x) == x) 代替。

Something like this?

[Flags]
public enum YourEnum
{
    One = 1, Two = 2, Four = 4, Eight = 8
}

// ...

YourEnum exampleData = YourEnum.Two | YourEnum.Four;

var values = Enum.GetValues(typeof(YourEnum))
                 .Cast<YourEnum>()
                 .Where(x => exampleData.HasFlag(x));

foreach (var v in values)
{
    Console.WriteLine(v);
}

Note that the HasFlag method is only available in .NET 4 -- if you're using an earlier version of the framework then you'll need to use Where(x => (exampleData & x) == x) instead.

北风几吹夏 2024-10-02 09:54:48

要获取枚举的所有值,请使用Enum.GetValues(typeof(myEnumeration))

System.Enum 还有其他方法这将允许您获取标识符(名称)并将名称与值之间进行转换。

注意。除非每个不同的值都具有不同的唯一位模式(即,当按位组合时不存在两个不同的子集或具有相同的值),否则某些转换将是不明确的。

To get all the values of an enumeration use Enum.GetValues(typeof(myEnumeration)).

System.Enum has other methods that will allow you to get the identifiers (names) and convert named to/from values.

NB. Unless every different value has a distinct unique bit pattern (i.e. there are no two distinct subsets that when combined with bitwise-or have the same value), some of the conversions will be ambiguous.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文