
在C语言中,枚举类型是被当做int或者unsigned int类型来处理的,所以按照C语言规范是没有办法遍历枚举类型的。
不过在一些特殊的情况下,可以实现有条件的遍历。
1 枚举类型必须连续。
由于枚举类型支持如下方式定义:
enum
{
ENUM_0,
ENUM_10 = 10,
ENUM_11
};
这样就会导致枚举类型不连续,这种枚举无法遍历。
2 枚举类型中人为加入起始及结束标记。
enum
{
ENUM_START,
ENUM_0,
ENUM_1,
ENUM_2,
ENUM_END
};
可以看到,在这个枚举类型中,人为加入可ENUM_START, 和ENUM_END。中间的ENUM_0,ENUM_1,ENUM_2才是实际的有效数据。当后续需要增删枚举元素时,也要保证ENUM_START, 和ENUM_END分别为最小和最大值。
满足以上两种条件下,就可以做枚举的遍历了,参考代码如下:
int i;
for(i = ENUM_START+1; i<ENUM_END; i ++)
{
//使用枚举元素。
}
这个循环中,i的值就是所有有效的枚举元素值。
问题:我有一个输入框,让用户只能从规定列表中选取值输入,那么我就要用enum,但是enum输出的值是它的value,value永远都是从0开始的递增数,我才不想要这个数值,我想要用户在列表中选取的的那个数值。
精简:即获取enum中的item值,而不是value。
思路:获取到item列表(string类型),用value去index这个列表,再转换为数值
If an enum control or indicator exists, create a property node for the enum and select the Strings[] property This property returns an array of strings for the specified enum Then use the Index Array function with the enum wired to the index terminal to access the desired string
If there is no enum control or indicator in the VI, wire the enum to the Format Into String function input; with a string value of %s wired to the format string input This function returns the string value associated with the input enum value
两种方法都画在下面
Public Function EnumNetworkAdapters() As String
Try
Dim query As SystemManagementManagementObjectSearcher = New SystemManagementManagementObjectSearcher("SELECT FROM Win32_NetworkAdapterConfiguration")
Dim queryCollection As SystemManagementManagementObjectCollection = queryGet()
Dim mo As New SystemManagementManagementObject
For Each mo In queryCollection
If IsDBNull(moItem("Description")) = False Then
EnumNetworkAdapters &= moItem("Description") & "|||"
End If
If IsDBNull(moItem("macaddress")) = False Then
If moItem("macaddress") <> "" Then
EnumNetworkAdapters &= moItem("macaddress") & "|||"
End If
End If
If IsDBNull(moItem("IPAddress")) = False Then
Dim temp As String
temp = Join(moItem("IPAddress"), "")
If temp <> "" Then
EnumNetworkAdapters &= temp & "|||"
End If
End If
Next
Catch err A ception
EnumNetworkAdapters = errMessage & errStackTrace
End Try
End Function
以上就是关于C语言怎样取得枚举型的名字 而不是值全部的内容,包括:C语言怎样取得枚举型的名字 而不是值、labview 中获取enum中的item信息,not value、vb.net怎么获取网卡MAC地址等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)