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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Get const-collection from code?

Get const-collection from code?

Scheduled Pinned Locked Moved C#
debuggingquestion
8 Posts 5 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.
  • A Offline
    A Offline
    Ariadne
    wrote on last edited by
    #1

    Hi, is there a way to list the contents of constants? ie. is there a function ConstantCollection()(see below) or something similar?

    class Test()
    {
    const string c1="blah1";
    const string c2="blah2";
    const string c3="blah3";
    const string c4="blah4";
    void Print()
    {
    foreach(object Constant in ConstantCollection(this))Debug.Print(Constant.Contents);
    }
    }
    Output:
    blah1
    blah2
    blah3
    blah4

    Ariadne

    D P R 3 Replies Last reply
    0
    • A Ariadne

      Hi, is there a way to list the contents of constants? ie. is there a function ConstantCollection()(see below) or something similar?

      class Test()
      {
      const string c1="blah1";
      const string c2="blah2";
      const string c3="blah3";
      const string c4="blah4";
      void Print()
      {
      foreach(object Constant in ConstantCollection(this))Debug.Print(Constant.Contents);
      }
      }
      Output:
      blah1
      blah2
      blah3
      blah4

      Ariadne

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

      Not that I know of, although if the constants are all of the same type then you could use an enum instead.

      Dave
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
      Why are you using VB6? Do you hate yourself? (Christian Graus)

      OriginalGriffO 2 Replies Last reply
      0
      • D DaveyM69

        Not that I know of, although if the constants are all of the same type then you could use an enum instead.

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        That won't work in this case - enum values cannot be strings.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        P 1 Reply Last reply
        0
        • A Ariadne

          Hi, is there a way to list the contents of constants? ie. is there a function ConstantCollection()(see below) or something similar?

          class Test()
          {
          const string c1="blah1";
          const string c2="blah2";
          const string c3="blah3";
          const string c4="blah4";
          void Print()
          {
          foreach(object Constant in ConstantCollection(this))Debug.Print(Constant.Contents);
          }
          }
          Output:
          blah1
          blah2
          blah3
          blah4

          Ariadne

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Using reflection:

          List<string> list = new List<string>();
          FieldInfo[] fis = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
          foreach (FieldInfo fi in fis)
          {
          if (fi.IsLiteral && !fi.IsInitOnly)
          list.Add(fi.Name);
          }
          return list;

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          A 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            That won't work in this case - enum values cannot be strings.

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            But it is possible to attach strings to enums using the Description attribute.

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            1 Reply Last reply
            0
            • D DaveyM69

              Not that I know of, although if the constants are all of the same type then you could use an enum instead.

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
              Why are you using VB6? Do you hate yourself? (Christian Graus)

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              :doh: I am an idiot! Provided the consts are single words, you can use them in an enum and use the names as const strings.

                  enum sample
                      {
                      Hello = 0,
                      There,
                      Last
                      }
              
                  static void Main(string\[\] args)
                      {
                      for (sample t = 0; t < sample.Last; t++)
                          {
                          Console.WriteLine(t);
                          }
                      Console.WriteLine("---");
                      }
              

              I think I'd rather define a class to hold my const strings and use a method to iterrate, though.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • A Ariadne

                Hi, is there a way to list the contents of constants? ie. is there a function ConstantCollection()(see below) or something similar?

                class Test()
                {
                const string c1="blah1";
                const string c2="blah2";
                const string c3="blah3";
                const string c4="blah4";
                void Print()
                {
                foreach(object Constant in ConstantCollection(this))Debug.Print(Constant.Contents);
                }
                }
                Output:
                blah1
                blah2
                blah3
                blah4

                Ariadne

                R Offline
                R Offline
                Rolando CC
                wrote on last edited by
                #7

                HI, I'm not sure if exist something like ConstantCollection, but this works for me (VB.net): Firts a simple class:

                Public Class Class1
                Public Const c1 As String = "c1"
                Public Const c2 As String = "c2"
                Const c3 As String = "c3"
                Dim var1 As String
                Public var2 As String

                Public Enum myEnum
                    value1
                    value2
                End Enum
                

                End Class

                For Each field As FieldInfo In getConstants(GetType(Class1))
                Console.WriteLine(field.Name)
                Next

                Private Function getConstants(ByVal pType As System.Type) As List(Of System.Reflection.FieldInfo)
                Dim fis() As FieldInfo
                Dim result As New List(Of System.Reflection.FieldInfo)

                    fis = pType.GetFields()
                
                    For Each fi As FieldInfo In fis
                        If fi.IsLiteral And Not fi.IsInitOnly Then
                            result.Add(fi)
                        End If
                    Next
                
                    Return result
                
                End Function
                

                This returns: c1 c2 The constants must be declared public. RolCr.

                1 Reply Last reply
                0
                • P Pete OHanlon

                  Using reflection:

                  List<string> list = new List<string>();
                  FieldInfo[] fis = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
                  foreach (FieldInfo fi in fis)
                  {
                  if (fi.IsLiteral && !fi.IsInitOnly)
                  list.Add(fi.Name);
                  }
                  return list;

                  "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                  As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                  My blog | My articles | MoXAML PowerToys | Onyx

                  A Offline
                  A Offline
                  Ariadne
                  wrote on last edited by
                  #8

                  Thanks Pete, that's what I'm looking for. It works fine.

                  Ariadne

                  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