473,769 Members | 5,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

OnAlarm(CallNum ber as BSTR,
AlarmType as atAlarmType,
NumberOfLocatio ns as long,
LocationIdentit y(NumberOfLocat ions) as array of VARIANT containing BSTR,
LocationType(Nu mberOfLocations ) as array of VARIANT containing atlocationType,
LocationTime(Nu mberOfLocations ) 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.InvalidC astException: 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 1183

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

Similar topics

21
1762
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. However i don't know how to get the contents of the array out? I am used to passing parameters to a function, and getting a single result back. Is there a way to get multiple results from a function? Is it something to do with passing byVal or byRef?
37
2463
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 cannot add them without first converting them to values. What might be causing this?
1
1317
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 my array into object before sending it to the function but i steel get an exception.... any ideas?
5
4500
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 the spreadsheet. I AM able to open an exiting Excel spreadsheet and do this with a single cell but not an array. In addition, I can send an array from the VB.NET program to a range of cells in Excel but I can't for the life of me figure out how...
20
3720
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 MyVariant = 2 Then MsgBox "MyVariant also equals the value 2" 160 If MyVariant = "" Then HowManyCopies = 1 170 If Not IsNumeric(MyVariant) Then HowManyCopies = 1 MsgBox "OK. HowManyCopies has a value of " & CStr(HowManyCopies) 180 For i =...
4
15007
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 array of strings? Public Function Split(ByVal strIn As String, Optional strDelimiter As String = " ") As String() Thanks
7
9538
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. This works well unless you have blank lines at the end of the CSV file. Now if you process the loop, you'll get an out of bounds subscript at the end of the loop because you are referencing null values at the end of the variant array. How...
9
4928
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 strReturnData As String = _ System.Text.Encoding.ASCII.GetString(receiveBytes)
17
11523
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 *array_string Thanks, -Kim
0
9424
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10000
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8879
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.