【问题标题】:Class property should be offered from another class (vb.net)应从另一个类(vb.net)提供类属性
【发布时间】:2015-11-29 12:05:07
【问题描述】:

我正在使用 vb.net 创建基于 Web 的应用程序。我尝试创建干净且易于使用的代码。

我的问题是: 我将创建一个具有属性的类,它只能具有常量值(可能来自另一个类),并且我希望设计器提供可接受的值。

I want to see like in the picture

示例: 我应该在下面的代码中更改什么?

Public Class users 
  private _gender as byte
  public writeonly property Gender as byte
    set(value as byte)
      _gender = value
    End set
  End property
End Class

Public Class genders
    Public ReadOnly Property Female As Byte
        Get
            Return 0
        End Get
    End Property
    Public ReadOnly Property Male As Byte
        Get
            Return 1
        End Get
    End Property
End Class   

【问题讨论】:

    标签: asp.net vb.net visual-studio


    【解决方案1】:

    使用Enum

    Public Enum Genders As Byte
        Female = 0
        Male = 1
    End Enum
    

    或者ConstShared 类的属性,如果您需要其他类型,那么整数

    Public Class SomeTypes
        Public Const TypeOne As String = "One"
        Public Const TypeTwo As String = "Two"
    
        Public Shared ReadOnly Property TypeThree As String = "Three"
    
    End Class
    

    【讨论】:

    • 非常简单的解决方案。我把它弄得太复杂了。感谢您的快速回答
    猜你喜欢
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    相关资源
    最近更新 更多