在VB.Net中定义字符串ENUM [英] Define String ENUM in VB.Net

查看:157
本文介绍了在VB.Net中定义字符串ENUM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的项目使用Window应用程序。有一种情况,我需要定义字符串枚举并在我的项目中使用它。



ie

  Dim PersonalInfo As String =个人信息
Dim Contanct As String =Personal Contanct

公开枚举测试
PersonalInfo
Contanct
结束枚举

现在我想要该变量PersonalInfo和Contract的值作为个人信息和个人关系。



如何使用ENUM获取此值?或者任何其他方式来做。



提前感谢...

解决方案

您可以创建一个新类型

 '''< completionlist cref =Test/> 
类测试

私钥作为字符串

公共共享ReadOnly联系人作为测试=新测试(个人连接)
公共共享只读个人信息测试=新测试(个人信息)

私有子新(键as String)
Me.Key =键
End Sub

公开覆盖函数ToString()As String
返回Me.Key
结束函数
结束类

,当您使用它时,它像枚举一样

  Sub Main 

DoSomething(Test.Contact)
DoSomething(Test.PersonalInfo)

End Sub

Sub DoSomething(test As Test )
Console.WriteLine(test.ToString())
End Sub

输出:


个人连接

个人信息



I am using Window Application for my project. There is situation where i need to define string enum and using it in my project.

i.e.

Dim PersonalInfo As String = "Personal Info"
Dim Contanct As String = "Personal Contanct"

    Public Enum Test
        PersonalInfo
        Contanct
    End Enum

Now i want value of that variable PersonalInfo and Contract as "Personal Info" and "Personal Contanct".

How can i get this value using ENUM? or anyother way to do it.

Thanks in advance...

解决方案

You could just create a new type

''' <completionlist cref="Test"/>
Class Test

    Private Key As String

    Public Shared ReadOnly Contact  As Test = New Test("Personal Contanct")
    Public Shared ReadOnly PersonalInfo As Test = New Test("Personal Info")

    Private Sub New(key as String)
        Me.Key = key
    End Sub

    Public Overrides Function ToString() As String
        Return Me.Key
    End Function
End Class

and when you use it, it kinda looks like an enum:

Sub Main

    DoSomething(Test.Contact)
    DoSomething(Test.PersonalInfo)

End Sub

Sub DoSomething(test As Test)
    Console.WriteLine(test.ToString())
End Sub

output:

Personal Contanct
Personal Info

这篇关于在VB.Net中定义字符串ENUM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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