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. C#
  4. enum use in combobox

enum use in combobox

Scheduled Pinned Locked Moved C#
helptutorialquestion
5 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.
  • D Offline
    D Offline
    Deresen
    wrote on last edited by
    #1

    Hey everyone, I'm trying to use an enum. But I don't use this a lot and I'm now at a point where I don't understand it anymore. I have this code:

    enum frequency
    {
    Second,
    Minute,
    Hour,
    Day,
    Month,
    Year
    }

    public constructor()
    {
    comBoxFrequency.Items.Add(frequency.Second);
    comBoxFrequency.Items.Add(frequency.Minute);
    comBoxFrequency.Items.Add(frequency.Hour);
    comBoxFrequency.Items.Add(frequency.Day);
    comBoxFrequency.Items.Add(frequency.Month);
    comBoxFrequency.Items.Add(frequency.Year);
    }
    public void buttonClick()
    {
    switch (comBoxFrequency.SelectedText)
    {
    case frequency.Second://Error: Cannot implicitly convert type '___.frequency' to 'string'
    //do something
    break;
    case frequency.Minute.ToString()://Error: A constant value is expected
    //do something
    break;
    case frequency.Hour:
    //do something
    break;
    case frequency.Day:
    //do something
    break;
    case frequency.Month:
    //do something
    break;
    case frequency.Year:
    //do something
    break;
    }
    }

    As you see I get two errors: Cannot implicitly convert type '___.frequency' to 'string' And if I make a string of this: A constant value is expected Does anyone know how to work around this problem (or what I do wrong)?

    M S 2 Replies Last reply
    0
    • D Deresen

      Hey everyone, I'm trying to use an enum. But I don't use this a lot and I'm now at a point where I don't understand it anymore. I have this code:

      enum frequency
      {
      Second,
      Minute,
      Hour,
      Day,
      Month,
      Year
      }

      public constructor()
      {
      comBoxFrequency.Items.Add(frequency.Second);
      comBoxFrequency.Items.Add(frequency.Minute);
      comBoxFrequency.Items.Add(frequency.Hour);
      comBoxFrequency.Items.Add(frequency.Day);
      comBoxFrequency.Items.Add(frequency.Month);
      comBoxFrequency.Items.Add(frequency.Year);
      }
      public void buttonClick()
      {
      switch (comBoxFrequency.SelectedText)
      {
      case frequency.Second://Error: Cannot implicitly convert type '___.frequency' to 'string'
      //do something
      break;
      case frequency.Minute.ToString()://Error: A constant value is expected
      //do something
      break;
      case frequency.Hour:
      //do something
      break;
      case frequency.Day:
      //do something
      break;
      case frequency.Month:
      //do something
      break;
      case frequency.Year:
      //do something
      break;
      }
      }

      As you see I get two errors: Cannot implicitly convert type '___.frequency' to 'string' And if I make a string of this: A constant value is expected Does anyone know how to work around this problem (or what I do wrong)?

      M Offline
      M Offline
      Mbah Dhaim
      wrote on last edited by
      #2

      Deresen wrote:

      switch (comBoxFrequency.SelectedText)

      use switch ((frequency)comBoxFrequency.SelectedValue) instead hope it helps

      dhaim programming is a hobby that make some money as side effect :)

      D 1 Reply Last reply
      0
      • M Mbah Dhaim

        Deresen wrote:

        switch (comBoxFrequency.SelectedText)

        use switch ((frequency)comBoxFrequency.SelectedValue) instead hope it helps

        dhaim programming is a hobby that make some money as side effect :)

        D Offline
        D Offline
        Deresen
        wrote on last edited by
        #3

        Yes it did help! Thank you very much. Now I can throw my if()____else if()___else if() (ugly code) away.

        M 1 Reply Last reply
        0
        • D Deresen

          Yes it did help! Thank you very much. Now I can throw my if()____else if()___else if() (ugly code) away.

          M Offline
          M Offline
          Mbah Dhaim
          wrote on last edited by
          #4

          you're welcome

          dhaim programming is a hobby that make some money as side effect :)

          1 Reply Last reply
          0
          • D Deresen

            Hey everyone, I'm trying to use an enum. But I don't use this a lot and I'm now at a point where I don't understand it anymore. I have this code:

            enum frequency
            {
            Second,
            Minute,
            Hour,
            Day,
            Month,
            Year
            }

            public constructor()
            {
            comBoxFrequency.Items.Add(frequency.Second);
            comBoxFrequency.Items.Add(frequency.Minute);
            comBoxFrequency.Items.Add(frequency.Hour);
            comBoxFrequency.Items.Add(frequency.Day);
            comBoxFrequency.Items.Add(frequency.Month);
            comBoxFrequency.Items.Add(frequency.Year);
            }
            public void buttonClick()
            {
            switch (comBoxFrequency.SelectedText)
            {
            case frequency.Second://Error: Cannot implicitly convert type '___.frequency' to 'string'
            //do something
            break;
            case frequency.Minute.ToString()://Error: A constant value is expected
            //do something
            break;
            case frequency.Hour:
            //do something
            break;
            case frequency.Day:
            //do something
            break;
            case frequency.Month:
            //do something
            break;
            case frequency.Year:
            //do something
            break;
            }
            }

            As you see I get two errors: Cannot implicitly convert type '___.frequency' to 'string' And if I make a string of this: A constant value is expected Does anyone know how to work around this problem (or what I do wrong)?

            S Offline
            S Offline
            Scott Dorman
            wrote on last edited by
            #5

            You can also take a look at this article[^] for some alternative ways to do this.

            Scott Dorman

            Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


            Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

            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