473,395 Members | 1,652 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,395 software developers and data experts.

Getting values in C# from a COM Event sending an array of strings as a variant

4
I’m using a third-party ActicveX component that has the following description for one of its events:

OnAlarm(CallNumber as BSTR,
AlarmType as atAlarmType,
NumberOfLocations as long,
LocationIdentity(NumberOfLocations) as array of VARIANT containing BSTR,
LocationType(NumberOfLocations) as array of VARIANT containing atlocationType,
LocationTime(NumberOfLocations) as arrey of VARIANT containing BSTR)
They mean by this that LocationType is an array of VARIANTS (containing enumeration)

When the OnAlarm Event is raised I have problems getting the 3 last values from it.

I have ported my application from VB.Net to C# and this is the only problem I can’t figure out how to solve in doing so.
In VB.Net it’s very easy to access these values as long as you don’t have the strong typing feature enabled. But not in C# where VARIANT type does not exist and is rather referenced as an object.

This is how OnAlarm looks in C#:

Expand|Select|Wrap|Line Numbers
  1. void OnAlarm(string CallNumber, ATOAC2Lib.atAlarmType AlarmType, int NumberOfLocations, ref object LocationIdentity, ref object LocationType, ref object LocationTime)
  2.     Member of ATOAC2Lib._IatAlarm3Events
In VB.Net the code I was using looked like this:
Expand|Select|Wrap|Line Numbers
  1.  Private Sub atAlarm2_OnAlarm1(ByVal CallNumber As String, ByVal AlarmType As ATOAC2Lib.atAlarmType, ByVal NumberOfLocations As Integer, ByRef LocationIdentity As Object, ByRef LocationType As Object, ByRef LocationTime As Object) Handles atAlarm2.OnAlarm
  2.  
  3.         Dim ret, i As Integer
  4.         Dim SecondLocationTime, FirstLocationTime, SecondLocation, FirstLocation, s As String
  5.  
  6.         Try
  7.             If AlarmType <> ATOAC2Lib.atAlarmType.atAlarmType_Test Then
  8.                 FirstLocationTime = "0"
  9.                 SecondLocationTime = "0"
  10.                 SecondLocation = "0"
  11.                 FirstLocation = "0"
  12.  
  13.                 For i = 0 To NumberOfLocations - 1 
  14.                     If i = 0 Then
  15.                         If LocationType(i) = ATOAC2Lib.atLocationType.atLocationType_System900_Locator Then
  16.                             FirstLocation = HexToDec(LocationIdentity(i))
  17.                             FirstLocationTime = LocationTime(i)
  18.                         End If
  19.                     ElseIf i = 1 Then
  20.                         mylogfile.writelog(3, "AL2-" & LocationIdentity(i))
  21.                         If LocationType(i) = ATOAC2Lib.atLocationType.atLocationType_System900_Locator Then
  22.                             SecondLocation = HexToDec(LocationIdentity(i))
  23.                             SecondLocationTime = LocationTime(i)
  24.                         End If
  25.                     End If
  26.                 Next i
  27. Etc.
  28.  
This is how the same method looks in C# after I converted it:
Expand|Select|Wrap|Line Numbers
  1.  private void atAlarm2_OnAlarm1(string CallNumber, ATOAC2Lib.atAlarmType AlarmType, int NumberOfLocations, ref object LocationIdentity, ref object LocationType, ref object LocationTime)
  2.         {
  3.             int i;
  4.             string SecondLocationTime;
  5.             string FirstLocationTime;
  6.             string SecondLocation;
  7.             string FirstLocation;
  8.  
  9.             String[] tempLocationIdentity;
  10.             ATOAC2Lib.atLocationType[] tempLocationType;
  11.             String[] tempLocationTime;
  12.  
  13.             try
  14.             {
  15.                 logger.Debug("Event atAlarm2_OnAlarm1 raised");
  16.  
  17.                 tempLocationIdentity = (String[])LocationIdentity;
  18.                 tempLocationType = (ATOAC2Lib.atLocationType[])LocationType;
  19.                 tempLocationTime = (String[])LocationTime;
  20.  
  21.                 if (AlarmType != ATOAC2Lib.atAlarmType.atAlarmType_Test)
  22.                 {
  23.                     FirstLocationTime = "0";
  24.                     SecondLocationTime = "0";
  25.                     SecondLocation = "0";
  26.                     FirstLocation = "0";
  27.                     AL_MsgCounter++;
  28.                     UpdateLabelText(this.LabelALCount, AL_MsgCounter.ToString());
  29.  
  30.                     // Extract First/Second Location and First/Second Time
  31.                     for (i = 0; i <= NumberOfLocations - 1; i++)
  32.                     {
  33.                         if (i == 0)
  34.                         {
  35.                             logger.Debug("First alarm location at " + tempLocationIdentity[i]);
  36.                             //mylogfile.WriteLog(3, "AL1-" + tempLocationIdentity[i]);
  37.                             if (tempLocationType[i] == ATOAC2Lib.atLocationType.atLocationType_System900_Locator)
  38.                             {
  39.                                 FirstLocation = HexToDec(tempLocationIdentity[i]);
  40.                                 FirstLocationTime = tempLocationTime[i];
  41.                             }
  42.                         }
  43.                         else if (i == 1)
  44.                         {
  45.                             logger.Debug("Second alarm location at " + tempLocationIdentity[i]);
  46.                             //mylogfile.WriteLog(3, "AL2-" + tempLocationIdentity[i]);
  47.                             if (tempLocationType[i] == ATOAC2Lib.atLocationType.atLocationType_System900_Locator)
  48.                             {
  49.                                 SecondLocation = HexToDec(tempLocationIdentity[i]);
  50.                                 SecondLocationTime = tempLocationTime[i];
  51.                             }
  52.                         }
  53.                     }
  54. Etc.
This code compiles with no problem, but running the application returns the following exception:
System.InvalidCastException: Unable to cast object of type ‘System.Object[]’ to type ‘System.String[]’.
Anyone that knows how to solve this?
I'm very thankful for all feedback.
Apr 12 '07 #1
0 1165

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

Similar topics

21
by: Gerry Abbott | last post by:
Hi All, If completed a script which parses a string into fragments (fields), then assigns these substrings into an array. I wish to turn this into a function to which i can pass the string....
37
by: MLH | last post by:
For example: Nz(,0) returns "300" if the value in field is 300 (currency data type) and "0" if the value is zero or null. I get strings in the query output - they are all left aligned and I...
1
by: pini_soos | last post by:
Hi there, i writing a c# application which call to a com object written in vb. the function in this object which i call gets as parameter: a variant array and a variant dsnstring. i tried to cast...
5
by: Lee | last post by:
Hi All, My problem is trying to figure out how to get the numbers from a range of cells in Excel into an array (double) in a VB.NET program, do something with the array, and then send it back to...
20
by: MLH | last post by:
120 MyString = "How many copies of each letter do you need?" 150 MyVariant = InputBox(MyString, "How Many?", "3") If MyVariant = "2" Then MsgBox "MyVariant equals the string '2'" If...
4
by: John | last post by:
Hi I need to return an array of string in my own split function (access 97). I have defined the function as below but I get err on 'As String()'. What can I do to make the function return an...
7
by: Paul M. Cook | last post by:
Let's say you have a CSV file and you load it into a variant array using the split function on VBCrLF. Then you load a variable with the line count and loop through the array for 0 to line count. ...
9
by: Miro | last post by:
VB 2003 at the end of the code, this works great. bytCommand = Encoding.ASCII.GetBytes("testing hello send text") udpClient.Send(bytCommand, bytCommand.Length) and this recieves it Dim...
17
by: kleary00 | last post by:
Hi, I am writing a function that needs to return an array of strings and I am having some trouble getting it right. I need some help. Here is what I consider an array of 100 strings: char...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.