简体   繁体   English

VarType到GetType()

[英]VarType to GetType()

I am converting a VB.NET project to C# and I got stuck on this if-loop, could anyone help me convert this? 我将VB.NET项目转换为C#,却陷入了if循环中,有人可以帮我转换吗? I am assuming that i'll need to use the GetType()-method for this but I'm not sure how. 我假设我需要为此使用GetType()方法,但我不确定如何使用。

The Loop: 循环:

Private Sub oOpcGroup_DataChange(ByVal TransactionID As Integer, ByVal NumItems As Integer, ByRef ClientHandles() As Integer, ByRef ItemValues() As Object, ByRef Qualities() As Integer, ByRef TimeStamps() As Date)

    Dim i As Integer

    For i = 1 To NumItems
        If VarType(ItemValues(i)) And Not VariantType.Array Then
            txtSubValue.Text = ItemValues(i)
        Else
            MsgBox("Data type return error, returned array, expected single item", MsgBoxStyle.Critical, "Data Change Error")
            Exit Sub
        End If
    Next i
    Exit Sub
End Sub

This is what I have so far: 这是我到目前为止的内容:

void oOpcGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)    //Events OPCGroup
    {

        for (int i = 0; i < NumItems; i++)
        {
            if(){

            }
            else
            {
                MessageBox.Show("Expected single item.", "Data type return error.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

    }

Here is what I came up with, using the GetType() instead of the Information.VarType. 这是我想出的,使用GetType()而不是Information.VarType。

I did replace the MsgBox method call with a call to System.Windows.Forms.MessageBox.Show. 我确实将MsgBox方法调用替换为对System.Windows.Forms.MessageBox.Show的调用。

private void oOpcGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)  
{

    for (int i = 0; i < NumItems; i++)
    {
        var obj = ItemValues.GetValue(i);

        if (obj != null && !obj.GetType().IsArray)
        {
            txtSubValue.Text = obj.ToString();
        }
        else
        {
            System.Windows.Forms.MessageBox.Show("Data type return error, returned array, expected single item", "Data Change Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

Forget the automatic code converters, they frequently make mistakes in conversion, especially if they don't have full context available (declaration of ItemValues etc.) 忘记自动代码转换器,他们经常在转换中犯错误,特别是如果它们没有完整的上下文(ItemValues的声明等)时。

I think the original VB code is incorrect in this case. 我认为这种情况下原始的VB代码不正确。 The code will execute like this C# code: 该代码将像以下C#代码一样执行:

int i = 0;

for (i = 1; i <= NumItems; i++) {
    if ((Information.VarType(ItemValues[i]) & ~VariantType.Array) != 0) {
        txtSubValue.Text = ItemValues[i];
    } else {
        Interaction.MsgBox("Data type return error, returned array, expected single item", MsgBoxStyle.Critical, "Data Change Error");
        return;
    }
}

However, VariantType is not a flags enum, so the 'And Not' operation is not producing a meaningful result in either language. 但是,VariantType不是标志枚举,因此“ And Not”操作在两种语言中均不会产生有意义的结果。 Going by the text in the message box, the correct condition would be VarType(ItemValues(i)) <> VariantType.Array in VB, Information.VarType(ItemValues[i]) != VariantType.Array . 按照消息框中的文本,正确的条件是VB中的VarType(ItemValues(i)) <> VariantType.ArrayInformation.VarType(ItemValues[i]) != VariantType.Array

Avoiding the VB 6 compatiblity functions, you could write if (!(ItemValues[i] is Array)) . 避免使用VB 6兼容性函数,可以编写if (!(ItemValues[i] is Array))

If you want to keep using the VB 6 compatiblity functions in C# to make porting easier, you need to add a reference to Microsoft.VisualBasic.dll. 如果要继续使用C#中的VB 6兼容性功能来简化移植,则需要添加对Microsoft.VisualBasic.dll的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM