I ran into this interesting little issue when make a settings window for a SerialPort.
I have the following enum:
[code=c#]
public enum BaudRates : int
{
BR1200 = 1200,
BR2400 = 2400,
BR4800 = 4800,
BR9600 = 9600,
BR19200 = 19200,
BR3800 = 38400,
BR57600 = 57600,
BR115200 = 115200,
BR230400 = 230400,
}
[/code]
I use the Enum class to retreive an array collection
[code=c#]
Array BRs = Enum.GetValues( typeof(BaudRate s));
[/code]
Now the thought is that I roughly have a BaudRate[] (instead of just returning an Object, they return an Array which all <type>[] inherit from?)
So I figure since my ENUM type is also an int, I'll do this:
[code=c#]
int[] intBRs (int[])BRs;
[/code]
I was actually surprised it compiled and executed.
intBRs is now usable like a regular int[].
Here is where the confusion comes in. intBRs.GetType( ) shows type of BaudRate[] and not int[] as it was declared.
Looking at the debug window, it shows BR1200 BR2400 and etc anstead of 1200,2400, etc.
BUT I can say int a= intBRs[0]; and a has the value of 1200 just fine.
Now I know there is implicit conversion involved, but I want to use the intBRs as the datasource for a ComboBox and then set the SelectedItem to mySerialPort.Ba udRate.
It cannot do this since the datasource is using BR1200, BR2400.
I can get around it by typecasting mySerialPort.Ba udRate as a BaudRates, but I think the intBRs should have really been a int[] not a BaudRates[];
Work around:
[code=C#]
int[] intBRs = (int[])Enum.GetValues (typeof(BaudRat es));
cbBaudRate.Data Source = (int[])intBRs;
cbBaudRate.Sele ctedItem = (BaudRates)_por t.BaudRate;
[/code]
This has the effect of the listbox showing the ENUM names and not the values, which is a bit more confusing when looking at the choices in the listbox
Does anyone have any thoughts on the boxing/unboxing issues
I have the following enum:
[code=c#]
public enum BaudRates : int
{
BR1200 = 1200,
BR2400 = 2400,
BR4800 = 4800,
BR9600 = 9600,
BR19200 = 19200,
BR3800 = 38400,
BR57600 = 57600,
BR115200 = 115200,
BR230400 = 230400,
}
[/code]
I use the Enum class to retreive an array collection
[code=c#]
Array BRs = Enum.GetValues( typeof(BaudRate s));
[/code]
Now the thought is that I roughly have a BaudRate[] (instead of just returning an Object, they return an Array which all <type>[] inherit from?)
So I figure since my ENUM type is also an int, I'll do this:
[code=c#]
int[] intBRs (int[])BRs;
[/code]
I was actually surprised it compiled and executed.
intBRs is now usable like a regular int[].
Here is where the confusion comes in. intBRs.GetType( ) shows type of BaudRate[] and not int[] as it was declared.
Looking at the debug window, it shows BR1200 BR2400 and etc anstead of 1200,2400, etc.
BUT I can say int a= intBRs[0]; and a has the value of 1200 just fine.
Now I know there is implicit conversion involved, but I want to use the intBRs as the datasource for a ComboBox and then set the SelectedItem to mySerialPort.Ba udRate.
It cannot do this since the datasource is using BR1200, BR2400.
I can get around it by typecasting mySerialPort.Ba udRate as a BaudRates, but I think the intBRs should have really been a int[] not a BaudRates[];
Work around:
[code=C#]
int[] intBRs = (int[])Enum.GetValues (typeof(BaudRat es));
cbBaudRate.Data Source = (int[])intBRs;
cbBaudRate.Sele ctedItem = (BaudRates)_por t.BaudRate;
[/code]
This has the effect of the listbox showing the ENUM names and not the values, which is a bit more confusing when looking at the choices in the listbox
Does anyone have any thoughts on the boxing/unboxing issues
Comment