enum use in combobox
-
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)?
-
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)?
Deresen wrote:
switch (comBoxFrequency.SelectedText)
use
switch ((frequency)comBoxFrequency.SelectedValue)
instead hope it helpsdhaim programming is a hobby that make some money as side effect :)
-
Deresen wrote:
switch (comBoxFrequency.SelectedText)
use
switch ((frequency)comBoxFrequency.SelectedValue)
instead hope it helpsdhaim programming is a hobby that make some money as side effect :)
-
Yes it did help! Thank you very much. Now I can throw my if()____else if()___else if() (ugly code) away.
you're welcome
dhaim programming is a hobby that make some money as side effect :)
-
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)?
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