为什么Enum需要使用CType? [英] Why does Enum need to use a CType?

查看:201
本文介绍了为什么Enum需要使用CType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在vb.net中,我有一个枚举定义为整数:

In vb.net, I got an Enum defined As Integer:

    Enum enUtilityTypeDetailStudentEntryWorkflow As Integer
        enUTDSEW_Default = 379
        enUTDSEW_ApplicantRecordBook = 380
    End Enum

我想分配枚举直接到一个变量,但它不起作用。

I would like to assign the Enum directly to a variable but it does not work.

        EntryWorkflowLookUpEdit.EditValue = enUtilityTypeDetailStudentEntryWorkflow.enUTDSEW_Default

我最后使用CType并将其转换为整数。

I ended up using a CType and converting it to an Integer.

        EntryWorkflowLookUpEdit.EditValue = CType(enUtilityTypeDetailStudentEntryWorkflow.enUTDSEW_Default, Integer)

这个工作,但它增加了很多噪声的代码,似乎已经将枚举定义为整数,让我想,为什么我必须分配再次吗?

This works but it adds a lot of noise to the code and it seems like the Enum is already defined as an Integer, making me think, why do I have to assign it again?

有没有其他方法可以完成这一切呢?
我知道我在这里缺少一些东西。

Is there other ways to accomplish this without all the noise? I know I am missing something here.

感谢一堆。

推荐答案

枚举被定义为使用整数作为其底层类型。

The enum is defined as using an integer as its underlying type.

这是一个很好的东西,这是一个明确的转换IMO,因为这意味着您不要意外最终使用枚举值,其中您真正意味着仅使用一个整数。枚举的要点是有效地拥有不同的数字观点,并改变您所使用的视图应该是明确的。

It's a good thing that this is an explicit conversion IMO, because it means you don't accidentally end up using an enum value where you really meant to just use a straight integer. The point of enums is to effectively have different "views" of numbers - and changing which "view" you're using should be explicit.

我期待着会有

如果这成为您的重大问题,您可以随时写一个<$ c的扩展方法$ c> ToInt32 或类似的东西,刚刚执行演员。

If this becomes a significant issue for you, you could always write an extension method of ToInt32 or something like that, which just performed the cast.

如果我试图减少这段代码中的噪音,我会尝试找到一个比 enUtilityTypeDetailStudentEntryWorkflow 更短的名称,同时也符合.NET命名约定。

Personally if I were trying to reduce the noise in this code, I'd try to find a shorter name than enUtilityTypeDetailStudentEntryWorkflow - and one which conformed with the .NET naming conventions, at the same time.

这篇关于为什么Enum需要使用CType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆