Question

Ok, what am I doing wrong here? I am trying to rewrite some vb stuff to c# (so I can learn some c#), but am failing miserably.

Here is the code, I'll post what's happenning after it:

private object[] _PNs;
internal object[] ParameterNames { set { _PNs = value; } }

private object[] _PVs;
internal object[] ParameterValues { set { _PVs = value; } }

private Enumerations.DataType[] _DTs;
internal Enumerations.DataType[] ParameterDataTypes { set { _DTs = value; } }


protected void PrepareParams(SqlCommand objCmd)
{
    try
    {
        long _DataSize = 0;
        int _PCt = _PVs.GetUpperBound(0);
        Type _t_dt = _DTs.GetType();
        for (int i = 0; i <= _PCt; ++i)
        {
            if (_t_dt.IsArray)
            {
                switch (_DTs[i])
                {
                    case 0:
                    case 33:
                    case 6:
                    case 9:
                    case 13:
                    case 19:
                        _DataSize = 8;
                        break;
                    case 1:
                    case 3:
                    case 7:
                    case 10:
                    case 12:
                    case 21:
                    case 22:
                    case 23:
                    case 25:
                        _DataSize = Strings.Len(_PVs[i]);
                        break;
                    case 2:
                    case 20:
                        _DataSize = 1;
                        break;
                    case 5:
                        _DataSize = 17;
                        break;
                    case 8:
                    case 17:
                    case 15:
                        _DataSize = 4;
                        break;
                    case 14:
                        _DataSize = 16;
                        break;
                    case 31:
                        _DataSize = 3;
                        break;
                    case 32:
                        _DataSize = 5;
                        break;
                    case 16:
                        _DataSize = 2;
                        break;
                    case 15:
                        break;
                }
                // here
                objCmd.Parameters.Add(_PNs[i], _DTs[i], _DataSize).Value = _PVs[i]; 
            }
            else
            {
                // here
                objCmd.Parameters.AddWithValue(_PNs[i], _PVs[i]);
            }
        }
        _PNs = null;
        _PVs = null;
        _DTs = null;
    }
    catch (Exception ex)
    {
    }
}

EDIT: With the changes suggested in the answers I am now getting: Cannot implicitly convert int to ...DataType and Has some invalid arguments on the Parameter.Add methods

So I assume that my class properties for these are being declared incorrectly.

How can I fix this?

Here is the original VB code:

Protected Friend WriteOnly Property ParameterNames() As Object
    Set(ByVal value As Object)
        _PNs = value
    End Set
End Property
Private _PNs As Object

Protected Friend WriteOnly Property ParameterValues() As Object
    Set(ByVal value As Object)
        _PVs = value
    End Set
End Property
Private _PVs As Object

Protected Friend WriteOnly Property ParameterDataTypes() As DataType()
    Set(ByVal value As DataType())
        _DTs = value
    End Set
End Property
Private _DTs As DataType()


Private Sub PrepareParams(ByVal objCmd As Object)
    Try
        Dim _DataSize As Long
        Dim _PCt As Integer = _PVs.GetUpperBound(0)
        For i = 0 To _PCt
            If IsArray(_DTs) Then
                Select Case _DTs(i)
                    Case 0, 33, 6, 9, 13, 19
                        _DataSize = 8
                    Case 1, 3, 7, 10, 12, 21, 22, 23, 25
                        _DataSize = Len(_PVs(i))
                    Case 2, 20
                        _DataSize = 1
                    Case 5
                        _DataSize = 17
                    Case 8, 17, 15
                        _DataSize = 4
                    Case 14
                        _DataSize = 16
                    Case 31
                        _DataSize = 3
                    Case 32
                        _DataSize = 5
                    Case 16
                        _DataSize = 2
                    Case 15
                End Select
                objCmd.Parameters.Add(_PNs(i), _DTs(i), _DataSize).Value = _PVs(i)
            Else
                objCmd.Parameters.AddWithValue(_PNs(i), _PVs(i))
            End If
        Next
        Erase _PNs : Erase _PVs : Erase _DTs
    Catch ex As Exception

    End Try
End Sub
Était-ce utile?

La solution

Array access in C# uses braces [] not parenthesis ().

Opening parenthesis would only follow an identifier in C# if that identifier is a method that is being invoked, which is why you're getting errors that it can't find those methods.

Autres conseils

In c#, to access elements of an array or collection you use the square bracket notation [n] where n is the index.

So _DTs(i) becomes _DTs[i] etc

_DTs(i) should be _DTs[i]

VB uses () for arrays, C# uses []

I expect you dont have Option Explicit on the VB side code, which means the compiler will 'put in' some cast / CType statements for you.

When you switch through the enum you need to compare it the enum 'Names' not the numbers OR explicitly cast, ie if you are getting:

'Cannot implicitly convert type 'int' to 'DataType'. An explicit conversion exists (are you missing a cast?)'

If you want to keep code largely as-is explicitly cast it to int, ie:

 switch  ((int)_DTs[i])

Or compare to the enum values, ie:

public enum DataType  { bah, hum }

Needs

case DataType.bah:
case DataType.hum:

and you MUST switch on ((int)_DTs[i]) if you want to compare to numbers

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top