0

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
Kevin
  • 2,684
  • 6
  • 35
  • 64

4 Answers4

6

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.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • this breaks things even further – Kevin Oct 17 '13 at 16:04
  • @o7thWebDesign You may well have other problems with your code that the compiler didn't even get a chance to complain about because it couldn't get past this problem. It is still correct to use braces for array access though. Looking at your code, you appear to not have been using explicit typing, and now that you're *forced* to use it it's biting you. You have data typed to `object` when it should really be a more specific type. – Servy Oct 17 '13 at 16:05
  • @o7thWebDesign Change ALL of the array indexes to use brackets instead of parentheses. I see 7 places where you need to do that. – Douglas Barbin Oct 17 '13 at 16:07
  • I have, and then more errors occurred. Please re-see my question as I've updated it. – Kevin Oct 17 '13 at 16:12
  • @o7thWebDesign This is what happens when you just store everything as an `object` instead of what it's actual type is. – Servy Oct 17 '13 at 16:13
  • It can be variable data types that these can `objects` can be... anywhere from strings, to longs, to nulls – Kevin Oct 17 '13 at 16:15
  • @o7thWebDesign In the places where the compiler isn't yelling at you, yes. In the places where it is yelling at you, no. – Servy Oct 17 '13 at 16:16
  • So, you are saying that I should cast the `objects` as something other than a generic `object`, when I have absolutely no idea what datatype they could be? – Kevin Oct 17 '13 at 16:18
  • @o7thWebDesign No, I'm saying that the parameter names aren't *allowed* to be any type of object. The must be strings, and so that should be the type of the object *througout*. The parameter values can be anything, which is why the compiler never complains about them anywhere. – Servy Oct 17 '13 at 16:19
  • objCmd.Parameters.Add(_PNs[i], _DTs[i], _DataSize).Value = _PVs[i]; May be worth changing this line to cast _DTs[i] to the SqlDbType type. I assume that is what it is supposed to be, also cast _PNs[i] to string. – Purplegoldfish Oct 17 '13 at 16:25
  • gotcha. One more error. `Has some invalid arguments` on `Parameters.Add` I also went and added (int) to `DTs` to cure the `implicit conversion` error – Kevin Oct 17 '13 at 16:26
  • @Purplegoldfish They shouldn't be cast, the variables should just be those types to begin with. There's no reason for them to ever have instances of any other type. – Servy Oct 17 '13 at 16:26
  • right, but in VB I am able to convert eh actual SqlDataTypes to its int representation. Am I right in assuming that is not true for C#? – Kevin Oct 17 '13 at 16:27
  • @o7thWebDesign So look at the parameters that you need to have, see what you're currently passing in, and determine which don't match. As I've said before, many of your variables are not of the right type. You should be changing their types accordingly. – Servy Oct 17 '13 at 16:27
  • @o7thWebDesign You can, but you really shouldn't be most of the time. At least I see no reason to in this code. Imagine how much more readable this code would be if the cases were each named data types, rather than random numbers that I have no idea what types they correspond to. – Servy Oct 17 '13 at 16:28
  • right, but that;s the point of the `Enumeration.DataType` to make them human readable... which now that I think more about it, it is kind of redonculous – Kevin Oct 17 '13 at 16:30
  • @o7thWebDesign And you ruin that readability by converting the value to an int. If you leave it as the enumeration values it's actually human readable. Also, I have no idea why you're using a custom enumeration and not the `SqlDbType` enumeration that is already defined for you. – Servy Oct 17 '13 at 16:32
  • agreed. I don't either LOL. Thanks for the help and discussion! – Kevin Oct 17 '13 at 16:33
3

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

Ric
  • 12,855
  • 3
  • 30
  • 36
2

_DTs(i) should be _DTs[i]

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

Purplegoldfish
  • 5,268
  • 9
  • 39
  • 59
0

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

tolanj
  • 3,651
  • 16
  • 30
  • You actually *can* switch on an enumeration, because every enumeration value will always resolve to a compile time constant. – Servy Oct 17 '13 at 16:29
  • Yes but this '_DTs' IS an array of Enumerations.DataType NOT an Enum Value.. ..arhh You are assuming Enumerations.DataType IS a real Enum and you may well be right. – tolanj Oct 17 '13 at 16:34
  • `Enumerations.DataType` **is** an enumeration...which is why it's inside of an `Enumerations` class/namespace... – Servy Oct 17 '13 at 16:36
  • I was fooled (perhaps) Cannot implicitly convert int to ...DataType line from OP, with an public enum DataType the code abve compiles for me, given OP is getting an error I assumed it was NOT a'real' enum but a 'fake' one – tolanj Oct 17 '13 at 16:43
  • The issue is that what you `switch` on must match what you put in your `case` statements. The solution is to make the case statements use the enum values, not to cast the enum value to an int. – Servy Oct 17 '13 at 16:45
  • I completely agree that is the correct solution, but in RL might I cast the enum rather than spending 15 mins looking up the ~32 out of order case ints and translating them to names....I have no illusions on what I would actually do. – tolanj Oct 17 '13 at 16:51