473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Problem converting from object to enum in C#

4
Hi,

I'll try and explain my problem.

I'm simulating an incoming event that in my application I get from a COM object. My problem is that I can't seem to figure out how to convert an object back to enum.

Here is my simulated event that I in real life receive through a COM object.

Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, System.EventArgs e)
  2. {
  3.             //atLocationType is of type enum
  4.             atLocationType[] t = new atLocationType[3];
  5.             t[0] = atLocationType.atLocationType_System900_Locator;
  6.             t[1] = atLocationType.atLocationType_SystemDECT_Signpost;
  7.             t[2] = atLocationType.atLocationType_Manual;
  8.             object p = t;
  9.  
  10.             myalarm_OnAlarm(3, ref p); 
  11. }
My event handler methode looks like this.

Expand|Select|Wrap|Line Numbers
  1. private void myalarm_OnAlarm(int NumberOfLocations, ref object LocationType)
  2. {
  3.             //atLocationType is of type enum
  4.             atLocationType tempLocationType;
  5.  
  6.             int i;
  7.             Console.WriteLine (NumberOfLocations);
  8.             for (i = 0; i < NumberOfLocations; i++)
  9.             {
  10.  
  11.                 tempLocationType = (ATOAC2Lib.atLocationType)((Object[])LocationType)[i];
  12.                 Console.WriteLine(tempLocationType);
  13.             }
  14. }
I get a cast error when the event is triggered when I run this. This is the error:
Unable to cast object of type 'atLocationType[]' to type 'System.Object[]'.

If I use a String instead of an enum in both the event and the event handler everything works fine though.

Any idea about how I can get the enum value back from object?

Thanks
Apr 17 '07 #1
2 10503
MikalE
4
My event handler methode above is supposed to look like this, sorry about the mistake.

Expand|Select|Wrap|Line Numbers
  1. private void myalarm_OnAlarm(int NumberOfLocations, ref object LocationType)
  2. {
  3.             //atLocationType is of type enum
  4.             atLocationType tempLocationType;
  5.  
  6.             int i;
  7.             Console.WriteLine (NumberOfLocations);
  8.             for (i = 0; i < NumberOfLocations; i++)
  9.             {
  10.  
  11.                 tempLocationType = (atLocationType)((Object[])LocationType)[i];
  12.                 Console.WriteLine(tempLocationType);
  13.             }
  14. }
Apr 17 '07 #2
MikalE
4
Just wanted to share the solution I found to the problem:

Expand|Select|Wrap|Line Numbers
  1. tempLocationType = ((atLocationType[])LocationType)[i];
Apr 17 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: Chiller | last post by:
I am in the process of writing a class that will represent metric distances by accepting a value (ie, 3) and a unit of measure (ie, m). I've written my constructor in the .h file as Distance...
3
by: Pete Davis | last post by:
I've never done this in C# so I don't know what the appropriate way of doing it is. I've got an array of bytes and I need to convert the array into "usable" data. For example, the first 4 bytes...
5
by: gmccallum | last post by:
I am trying to convert the value of a string to a defined enum value such as follows. public enum MyEnum { One, Two }; string MyString = "One"; // or even this is fine string MyString2 =...
2
by: Robert W. | last post by:
In a posting earlier this year I found a simple approach to convert a string to a particular Enum value. The one line solution looked like this: MyEnum ConvertedString = (MyEnum)...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
1
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry =...
4
by: sonu | last post by:
Hi, I have a problem regarding use of a ListviewSorter class which is used to sort the items in the listview. Basically I have a listview with three columns as "Name","Size" and...
9
by: Mircea | last post by:
Hi everybody, I have a big problem. I am trying to use serial communication in C# 2. The problem is that the event DataReceived is never fired. I have tried on two computers and it does not...
10
by: Doug | last post by:
I have an enum that will look kind of like this: private enum MyEnum { SomeValue = 0, AnotherValue = 1, ThirdValue = 2 }
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.