|
-
Dec 20th, 2005, 05:01 AM
#1
Convert VB.Net to C#
Hello
This is a bit of a big ask, but I have taken the leap to try and create an Application, which I have working in VB.NET 2005, to C# 2005, and to be honest I am struggling!!!
Can anyone suggest an equivalent way of writing the following method in C#
VB Code:
Private Sub ConnectedGroup_DataChange(ByVal TransactionID As Integer, ByVal NumItems As Integer, ByRef ClientHandles As System.Array, ByRef ItemValues As System.Array, ByRef Qualities As System.Array, ByRef TimeStamps As System.Array) Handles ConnectedGroup.DataChange
' We don't have error handling here since this is an event called from the OPC interface
Try
Dim i As Short
For i = 1 To NumItems
' Use the 'Clienthandles' array returned by the server to pull out the
' index number of the control to update and load the value.
If IsArray(ItemValues(i)) Then
Dim ItsAnArray As Array
Dim x As Integer
Dim Suffix As String
ItsAnArray = ItemValues(i)
' Store the size of array for use by sync write
OPCItemIsArray(ClientHandles(i)) = ItsAnArray.GetUpperBound(0) + 1
OPCItemValue(ClientHandles(i)).Text = ""
For x = ItsAnArray.GetLowerBound(0) To ItsAnArray.GetUpperBound(0)
If x = ItsAnArray.GetUpperBound(0) Then
Suffix = ""
Else
Suffix = ", "
End If
OPCItemValue(ClientHandles(i)).Text = _
OPCItemValue(ClientHandles(i)).Text & ItsAnArray(x) & Suffix
Next x
Else
OPCItemValue(ClientHandles(i)).Text = ItemValues(i)
End If
Next i
Catch ex As Exception
' Error handling
MessageBox.Show("OPC DataChange failed with exception: " + ex.Message, "SimpleOPCInterface Exception", MessageBoxButtons.OK)
End Try
End Sub
I have tried a number of different things, but cannot find an "equivalent" of the VB IsArray method. And also when I pass the Array into the method ByRef I do not seem to be able to access the members of that array in the way that I would expect i.e.
ItemValues[i] = something
I get the error:
Cannot apply indexing with [] to an expression of type 'System.Array'
Any ideas?!?!?
Thank you for your help!!
Gary
-
Dec 20th, 2005, 05:27 AM
#2
Re: Convert VB.Net to C#
Code:
private void ConnectedGroup_DataChange(
int TransactionID,
int NumItems,
ref Array ClientHandles,
ref Array ItemValues,
ref Array Qualities,
ref Array TimeStamps
)
{
// We don't have error handling here since this is an event called from the OPC interface
try
{
for (int i = 1; i <= NumItems; i++)
{
/* Use the 'Clienthandles' array returned by the server to pull out the
index number of the control to update and load the value. */
if (ItemValues[i] is Array)
{
Array ItsAnArray = ItemValues(i);
// Store the size of array for use by sync write
OPCItemIsArray[ClientHandles[i]] = ItsAnArray.GetUpperBound(0) + 1;
OPCItemValue[ClientHandles[i]].Text = "";
for (int x = ItsAnArray.GetLowerBound(0); x <= ItsAnArray.GetUpperBound(0); x++)
{
OPCItemValue[ClientHandles[i]].Text +=
ItsAnArray[x] + ((x == ItsAnArray.GetUpperBound(0)) ? "" : ", ");
}
}
else
{
OPCItemValue[ClientHandles[i]].Text = ItemValues[i];
}
}
}
catch (Exception ex)
{
// Error handling
MessageBox.Show("OPC DataChange failed with exception: " + ex.Message,
"SimpleOPCInterface Exception",
MessageBoxButtons.OK);
}
}
I think
Last edited by penagate; Dec 20th, 2005 at 07:51 AM.
-
Dec 20th, 2005, 06:23 AM
#3
Re: Convert VB.Net to C#
Thank you for your help! It is much appreciated!
I have tried the code that you provided and I am getting one error with it:
Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
On line:
VB Code:
if (typeof(ItemValues[i]) == typeof(Array))
Any thoughts!
Gary
-
Dec 20th, 2005, 07:34 AM
#4
Re: Convert VB.Net to C#
I know it's a bit late now.. but instead of converting your project from VB to C# why didn't you just put majority of your VB code into a DLL and just use both VB and C# ? or the other way around (part that requires C# in a dll) ?
-
Dec 20th, 2005, 07:47 AM
#5
Re: Convert VB.Net to C#
try
Code:
if (ItemValues[i] is Array)
Edit: updated code above
-
Dec 20th, 2005, 08:59 AM
#6
Re: Convert VB.Net to C#
Can you tell me why I am having to do this (which works):
VB Code:
OPCItemValue[(int)ClientHandles.GetValue(i)].Text = ItemValues.GetValue(i);
when you have suggested that this should work:
VB Code:
OPCItemValue[ClientHandles[i]].Text = ItemValues[i];
Why can't I reference the Array Member directly?? I know this works in VB.NET, is this one of the differences that I am going to have to learn about?
Thanks again!!
Gary
-
Dec 20th, 2005, 09:12 AM
#7
Re: Convert VB.Net to C#
I don't really know, but why are you using the Array class in the first place? You can pass regular (derived from the Array class) arrays by reference too.
e.g.
Code:
int[] myArray = new int[3] {1, 2, 4};
creates an strongly typed array that derives from the System.Array class.
-
Dec 20th, 2005, 09:22 AM
#8
Re: Convert VB.Net to C#
Hello
Just to put you in the picture, I am using a third party library for the purposes of connecting to a Siemens S7 300 PLC via an OPC server so the datachanged event that I originally asked you to convert to C# is a call to the method within their library.
When I tried to pass in, for example int[] I was presented with an error about being unable to cast from int[] to System.Array. Have I missing something obvious in this step?!?!?
To solve this problem I have declared all my arrays as Array , rather than their type.
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|