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#: - void OnAlarm(string CallNumber, ATOAC2Lib.atAlarmType AlarmType, int NumberOfLocations, ref object LocationIdentity, ref object LocationType, ref object LocationTime)
-
Member of ATOAC2Lib._IatAlarm3Events
In VB.Net the code I was using looked like this: - 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
-
-
Dim ret, i As Integer
-
Dim SecondLocationTime, FirstLocationTime, SecondLocation, FirstLocation, s As String
-
-
Try
-
If AlarmType <> ATOAC2Lib.atAlarmType.atAlarmType_Test Then
-
FirstLocationTime = "0"
-
SecondLocationTime = "0"
-
SecondLocation = "0"
-
FirstLocation = "0"
-
-
For i = 0 To NumberOfLocations - 1
-
If i = 0 Then
-
If LocationType(i) = ATOAC2Lib.atLocationType.atLocationType_System900_Locator Then
-
FirstLocation = HexToDec(LocationIdentity(i))
-
FirstLocationTime = LocationTime(i)
-
End If
-
ElseIf i = 1 Then
-
mylogfile.writelog(3, "AL2-" & LocationIdentity(i))
-
If LocationType(i) = ATOAC2Lib.atLocationType.atLocationType_System900_Locator Then
-
SecondLocation = HexToDec(LocationIdentity(i))
-
SecondLocationTime = LocationTime(i)
-
End If
-
End If
-
Next i
-
Etc.
-
This is how the same method looks in C# after I converted it: - private void atAlarm2_OnAlarm1(string CallNumber, ATOAC2Lib.atAlarmType AlarmType, int NumberOfLocations, ref object LocationIdentity, ref object LocationType, ref object LocationTime)
-
{
-
int i;
-
string SecondLocationTime;
-
string FirstLocationTime;
-
string SecondLocation;
-
string FirstLocation;
-
-
String[] tempLocationIdentity;
-
ATOAC2Lib.atLocationType[] tempLocationType;
-
String[] tempLocationTime;
-
-
try
-
{
-
logger.Debug("Event atAlarm2_OnAlarm1 raised");
-
-
tempLocationIdentity = (String[])LocationIdentity;
-
tempLocationType = (ATOAC2Lib.atLocationType[])LocationType;
-
tempLocationTime = (String[])LocationTime;
-
-
if (AlarmType != ATOAC2Lib.atAlarmType.atAlarmType_Test)
-
{
-
FirstLocationTime = "0";
-
SecondLocationTime = "0";
-
SecondLocation = "0";
-
FirstLocation = "0";
-
AL_MsgCounter++;
-
UpdateLabelText(this.LabelALCount, AL_MsgCounter.ToString());
-
-
// Extract First/Second Location and First/Second Time
-
for (i = 0; i <= NumberOfLocations - 1; i++)
-
{
-
if (i == 0)
-
{
-
logger.Debug("First alarm location at " + tempLocationIdentity[i]);
-
//mylogfile.WriteLog(3, "AL1-" + tempLocationIdentity[i]);
-
if (tempLocationType[i] == ATOAC2Lib.atLocationType.atLocationType_System900_Locator)
-
{
-
FirstLocation = HexToDec(tempLocationIdentity[i]);
-
FirstLocationTime = tempLocationTime[i];
-
}
-
}
-
else if (i == 1)
-
{
-
logger.Debug("Second alarm location at " + tempLocationIdentity[i]);
-
//mylogfile.WriteLog(3, "AL2-" + tempLocationIdentity[i]);
-
if (tempLocationType[i] == ATOAC2Lib.atLocationType.atLocationType_System900_Locator)
-
{
-
SecondLocation = HexToDec(tempLocationIdentity[i]);
-
SecondLocationTime = tempLocationTime[i];
-
}
-
}
-
}
-
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.
0 1183 Sign in to post your reply or Sign up for a free account.
Similar topics |
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?
|
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?
|
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?
|
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...
|
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 =...
| |
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
|
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...
|
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)
|
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
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |