Results 1 to 8 of 8

Thread: Convert VB.Net to C#

  1. #1

    Thread Starter
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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:
    1. 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
    2.         ' We don't have error handling here since this is an event called from the OPC interface
    3.  
    4.         Try
    5.             Dim i As Short
    6.             For i = 1 To NumItems
    7.                 ' Use the 'Clienthandles' array returned by the server to pull out the
    8.                 ' index number of the control to update and load the value.
    9.                 If IsArray(ItemValues(i)) Then
    10.                     Dim ItsAnArray As Array
    11.                     Dim x As Integer
    12.                     Dim Suffix As String
    13.  
    14.                     ItsAnArray = ItemValues(i)
    15.  
    16.                     ' Store the size of array for use by sync write
    17.                     OPCItemIsArray(ClientHandles(i)) = ItsAnArray.GetUpperBound(0) + 1
    18.  
    19.                     OPCItemValue(ClientHandles(i)).Text = ""
    20.                     For x = ItsAnArray.GetLowerBound(0) To ItsAnArray.GetUpperBound(0)
    21.                         If x = ItsAnArray.GetUpperBound(0) Then
    22.                             Suffix = ""
    23.                         Else
    24.                             Suffix = ", "
    25.                         End If
    26.                         OPCItemValue(ClientHandles(i)).Text = _
    27.                          OPCItemValue(ClientHandles(i)).Text & ItsAnArray(x) & Suffix
    28.                     Next x
    29.                 Else
    30.                     OPCItemValue(ClientHandles(i)).Text = ItemValues(i)
    31.                 End If
    32.             Next i
    33.         Catch ex As Exception
    34.             ' Error handling
    35.             MessageBox.Show("OPC DataChange failed with exception: " + ex.Message, "SimpleOPCInterface Exception", MessageBoxButtons.OK)
    36.         End Try
    37.     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

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  3. #3

    Thread Starter
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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:
    1. if (typeof(ItemValues[i]) == typeof(Array))

    Any thoughts!

    Gary

  4. #4
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    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) ?

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Convert VB.Net to C#

    try
    Code:
    if (ItemValues[i] is Array)
    Edit: updated code above

  6. #6

    Thread Starter
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Convert VB.Net to C#

    Can you tell me why I am having to do this (which works):

    VB Code:
    1. OPCItemValue[(int)ClientHandles.GetValue(i)].Text = ItemValues.GetValue(i);

    when you have suggested that this should work:

    VB Code:
    1. 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

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  8. #8

    Thread Starter
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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
  •  



Click Here to Expand Forum to Full Width