Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Enums and Attributes

Enums and Attributes

Scheduled Pinned Locked Moved Visual Basic
tutorialhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rohancragg
    wrote on last edited by
    #1

    I can't for the life of me work out (or find samples on) how to read back the attribute of an entry in an Enum, for example: Public Enum Colours <Description="Bright Red"> BrightRed = 0 <Description="Brilliant Yellow"> BrightYellow = 1 <Description="Puce"> Horrible = 2 End Enum I know it has something to do with Reflection, but that doesnt seem to help :confused: Ultimately I want to write a function to able to do something like GetDescription(GetType(Colours)) Thanks, ro.

    D M 2 Replies Last reply
    0
    • R rohancragg

      I can't for the life of me work out (or find samples on) how to read back the attribute of an entry in an Enum, for example: Public Enum Colours <Description="Bright Red"> BrightRed = 0 <Description="Brilliant Yellow"> BrightYellow = 1 <Description="Puce"> Horrible = 2 End Enum I know it has something to do with Reflection, but that doesnt seem to help :confused: Ultimately I want to write a function to able to do something like GetDescription(GetType(Colours)) Thanks, ro.

      D Offline
      D Offline
      dynamic
      wrote on last edited by
      #2

      you mean something like this : VB:


      Public Enum Colours
      BrightRed = 0
      BrightYellow = 1
      Horrible = 2
      End Enum

      Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          Dim c As Colours
          MessageBox.Show(GetColours(c.BrightRed))
      End Sub
      
      Public Function GetColours(ByVal i As Integer) As String
          Dim clr As Colours
          Select Case i '/// numerical value of the colour.
              Case clr.BrightRed
                  Return "you chose BrightRed" '/// show the colour you chose.
              Case clr.BrightYellow
                  Return "you chose BrightYellow"
              Case clr.Horrible
                  Return "you chose Horrible"
          End Select
      
      End Function
      

      hope that helps a little:)


      Private void ExpectingTwins(string twins)
      {
      switch(twins)
      {
      Case ("twins on the way"):
      MessageBox.Show("for mr and mrs dynamic","twins on the way");
      break;
      }
      }


      1 Reply Last reply
      0
      • R rohancragg

        I can't for the life of me work out (or find samples on) how to read back the attribute of an entry in an Enum, for example: Public Enum Colours <Description="Bright Red"> BrightRed = 0 <Description="Brilliant Yellow"> BrightYellow = 1 <Description="Puce"> Horrible = 2 End Enum I know it has something to do with Reflection, but that doesnt seem to help :confused: Ultimately I want to write a function to able to do something like GetDescription(GetType(Colours)) Thanks, ro.

        M Offline
        M Offline
        Matt Casto
        wrote on last edited by
        #3

        The following function should work for you ... Imports System.ComponentModel Imports System.Reflection Private Function GetContentTypeValue(ByVal value As Colours) As String      Dim fi As FieldInfo = value.GetType.GetField(value.ToString)      Dim attributes As DescriptionAttribute() = CType(fi.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())      If attributes.Length > 0 Then           Return attributes(0).Value      Else           Return String.Empty      End If End Function

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups