将代码从C#转换为VB.NET时出现小错误

本文关键字:错误 NET VB 代码 转换 | 更新日期: 2023-09-27 18:25:01

我几乎已经完成了从C#到VB.NET的翻译,但我遇到了一些小错误:


  • "issuerId"已在此类中声明为"Private Shared issuerId()as String"。第17行
  • 无法将"字符串的一维数组"类型的值转换为"字符串"
    第215行
  • 无法将"字符串的一维数组"类型的值转换为"字符串"。第223行

原始代码

using System;
namespace sisow
{
public class Sisow
{
    private static string[] issuerid;
    private static string[] issuername;
    private static DateTime lastcheck;
    private string response;
    // Merchant data
    public string merchantId;
    public string merchantKey;
    public string shopId;           // voor toekomstige doeleinden
    // Transaction data
    public string payment;          // empty=iDEAL; ideal=iDEAL; sofort=DIRECTebanking; mistercash=MisterCash
    public string issuerId;         // mandatory; sisow bank code
    public string purchaseId;       // mandatory; max 16 alphanumeric
    public string entranceCode;     // max 40 strict alphanumeric (letters and numbers only)
    public string description;      // mandatory; max 32 alphanumeric
    public double amount;           // mandatory; min 0.45
    public string notifyUrl;
    public string returnUrl;        // mandatory
    public string cancelUrl;
    public string callbackUrl;
    // Status data
    public string status;
    public DateTime timeStamp;
    public string consumerAccount;
    public string consumerName;
    public string consumerCity;
    // Result/check data
    public string trxId;
    public string issuerUrl;
    // Error data
    public string errorCode;
    public string errorMessage;
    // Status
    public const string statusSuccess = "Success";
    public const string statusCancelled = "Cancelled";
    public const string statusExpired = "Expired";
    public const string statusFailure = "Failure";
    public const string statusOpen = "Open";
    public Sisow(string merchantid, string merchantkey)
    {
        this.merchantId = merchantid;
        this.merchantKey = merchantkey;
        shopId = "";
    }
    // voor toekomstige doeleinden
    public Sisow(string merchantid, string merchantkey, string shopid)
    {
        this.merchantId = merchantid;
        this.merchantKey = merchantkey;
        this.shopId = shopid;
    }
    private void error()
    {
        errorCode = parse("errorcode");
        errorMessage = System.Web.HttpUtility.UrlDecode(parse("errormessage"));
    }
    private string parse(string search)
    {
        return parse(response, search);
    }
    private string parse(string xml, string search)
    {
        int start, end;
        if ((start = xml.IndexOf("<" + search + ">")) < 0)
            return null;
        start += search.Length + 2;
        if ((end = xml.IndexOf("</" + search + ">", start)) < 0)
            return null;
        return xml.Substring(start, end - start);
    }
    private bool send(string method, params string[] keyvalue)
    {
        string parms = "";
        string url = "https://www.sisow.nl/Sisow/iDeal/RestHandler.ashx/" + method;
        try
        {
            for (int i = 0; i + 1 < keyvalue.Length; i += 2)
            {
                if (string.IsNullOrEmpty(keyvalue[i + 1]))
                    continue;
                if (!string.IsNullOrEmpty(parms))
                    parms += "&";
                parms += keyvalue[i] + "=" + System.Web.HttpUtility.UrlEncode(keyvalue[i + 1]);
            }
            System.Net.HttpWebRequest hwr = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            hwr.ContentType = "application/x-www-form-urlencoded";
            hwr.Method = "POST";
            hwr.ContentLength = parms.Length;
            System.IO.StreamWriter sw = new System.IO.StreamWriter(hwr.GetRequestStream());
            sw.Write(parms);
            sw.Flush();
            sw.Close();
            System.Net.HttpWebResponse hws = (System.Net.HttpWebResponse)hwr.GetResponse();
            System.IO.StreamReader sr = new System.IO.StreamReader(hws.GetResponseStream());
            response = sr.ReadToEnd();
            hws.Close();
            return true;
        }
        catch (Exception)
        {
            response = "";
            error();
            return false;
        }
    }
    private int getDirectory()
    {
        if (issuerid != null && lastcheck.AddDays(1).CompareTo(DateTime.Now) >= 0)
            return 0;
        if (!send("DirectoryRequest"))
            return -1;
        string search = parse("directory");
        if (string.IsNullOrEmpty(search))
        {
            error();
            return -2;
        }
        string[] iss = search.Replace("<issuer>", "").Split(new string[] { "</issuer>" }, StringSplitOptions.RemoveEmptyEntries);
        issuerid = new string[iss.Length];
        issuername = new string[iss.Length];
        for (int i = 0; i < iss.Length; i++)
        {
            issuerid[i] = parse(iss[i], "issuerid");
            issuername[i] = parse(iss[i], "issuername");
        }
        lastcheck = DateTime.Now;
        return 0;
    }
    // DirectoryRequest
    public int DirectoryRequest(bool test, out string select)
    {
        int ex;
        select = "<select id='"sisowbank'" name='"issuerid'">";
        ex = getDirectory();
        if (ex < 0)
            return ex;
        for (int i = 0; i < issuerid.Length; i++)
        {
            select += "<option value='"" + issuerid[i] + "'">" + issuername[i] + "</option>";
        }
        select += "</select>";
        return 0;
    }
    // DirectoryRequest
    public int DirectoryRequest(bool test, out string[] issuers)
    {
        int ex;
        issuers = null;
        ex = getDirectory();
        if (ex < 0)
            return ex;
        issuers = new string[issuerid.Length * 2];
        for (int i = 0; i < issuerid.Length; i++)
        {
            issuers[i * 2] = issuerid[i];
            issuers[i * 2 + 1] = issuername[i];
        }
        return 0;
    }
    // compute SHA1
    private static string GetSHA1(string key)
    {
        System.Security.Cryptography.SHA1Managed sha = new System.Security.Cryptography.SHA1Managed();
        System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
        byte[] bytes = sha.ComputeHash(enc.GetBytes(key));
        //string sha1 = System.BitConverter.ToString(sha1).Replace("-", "");
        string sha1 = "";
        for (int j = 0; j < bytes.Length; j++)
            sha1 += bytes[j].ToString("x2");
        return sha1;
    }
    // TransactionRequest
    public int TransactionRequest()
    {
        trxId = issuerUrl = "";
        if (string.IsNullOrEmpty(merchantId))
            return -1;
        if (string.IsNullOrEmpty(merchantKey))
            return -2;
        if (string.IsNullOrEmpty(purchaseId))
            return -3;
        if (amount < 0.45)
            return -4;
        if (string.IsNullOrEmpty(description))
            return -5;
        if (string.IsNullOrEmpty(returnUrl))
            return -6;
        if (string.IsNullOrEmpty(issuerId) && string.IsNullOrEmpty(payment))
            return -7;
        if (string.IsNullOrEmpty(entranceCode))
            entranceCode = purchaseId;
        string sha1 = GetSHA1(purchaseId + entranceCode + (amount * 100).ToString() + shopId + merchantId + merchantKey);
        if (!send("TransactionRequest", "shopid", shopId, "merchantid", merchantId, "payment", payment, "issuerid", issuerId, "purchaseid", purchaseId, 
            "amount", (amount * 100).ToString(), "description", description, "entrancecode", entranceCode, "returnurl", returnUrl,
            "cancelurl", cancelUrl, "callbackurl", callbackUrl, "notifyurl", notifyUrl, "sha1", sha1))
            return -8;
        trxId = parse("trxid");
        if (string.IsNullOrEmpty(trxId))
        {
            error();
            return -2;
        }
        issuerUrl = System.Web.HttpUtility.UrlDecode(parse("issuerurl"));
        return 0;
    }
    private int GetStatus()
    {
        status = parse("status");
        if (string.IsNullOrEmpty(status))
        {
            error();
            return -5;
        }
        timeStamp = DateTime.Parse(parse("timestamp"));
        amount = long.Parse(parse("amount")) / 100.0;
        consumerAccount = parse("consumeraccount");
        consumerName = parse("consumername");
        consumerCity = parse("consumercity");
        purchaseId = parse("purchaseid");
        description = parse("description");
        entranceCode = parse("entrancecode");
        return 0;
    }
    // StatusRequest
    public int StatusRequest()
    {
        if (string.IsNullOrEmpty(merchantId))
            return -1;
        if (string.IsNullOrEmpty(merchantKey))
            return -2;
        if (string.IsNullOrEmpty(trxId))
            return -3;
        string sha1 = GetSHA1(trxId + shopId + merchantId + merchantKey);
        if (!send("StatusRequest", "shopid", shopId, "merchantid", merchantId, "trxid", trxId, "sha1", sha1))
            return -4;
        return GetStatus();
    }
    // StatusRequest
    public int StatusRequest(string trxid)
    {
        if (string.IsNullOrEmpty(merchantId))
            return -1;
        if (string.IsNullOrEmpty(merchantKey))
            return -2;
        if (string.IsNullOrEmpty(trxid))
            return -3;
        trxId = trxid;
        string sha1 = GetSHA1(trxId + shopId + merchantId + merchantKey);
        if (!send("StatusRequest", "shopid", shopId, "merchantid", merchantId, "trxid", trxId, "sha1", sha1))
            return -4;
        return GetStatus();
    }
}
}

新VB代码

Namespace sisow
Public Class Sisow
    Private Shared issuerid As String()
    Private Shared issuername As String()
    Private Shared lastcheck As DateTime
    Private response As String
    ' Merchant data
    Public merchantId As String
    Public merchantKey As String
    Public shopId As String
    ' voor toekomstige doeleinden
    ' Transaction data
    Public payment As String
    ' empty=iDEAL; ideal=iDEAL; sofort=DIRECTebanking; mistercash=MisterCash
    Public issuerId As String
    ' mandatory; sisow bank code
    Public purchaseId As String
    ' mandatory; max 16 alphanumeric
    Public entranceCode As String
    ' max 40 strict alphanumeric (letters and numbers only)
    Public description As String
    ' mandatory; max 32 alphanumeric
    Public amount As Double
    ' mandatory; min 0.45
    Public notifyUrl As String
    Public returnUrl As String
    ' mandatory
    Public cancelUrl As String
    Public callbackUrl As String
    ' Status data
    Public status As String
    Public timeStamp As DateTime
    Public consumerAccount As String
    Public consumerName As String
    Public consumerCity As String
    ' Result/check data
    Public trxId As String
    Public issuerUrl As String
    ' Error data
    Public errorCode As String
    Public errorMessage As String
    ' Status
    Public Const statusSuccess As String = "Success"
    Public Const statusCancelled As String = "Cancelled"
    Public Const statusExpired As String = "Expired"
    Public Const statusFailure As String = "Failure"
    Public Const statusOpen As String = "Open"
    Public Sub New(merchantid As String, merchantkey As String)
        Me.merchantId = merchantid
        Me.merchantKey = merchantkey
        shopId = ""
    End Sub
    ' voor toekomstige doeleinden
    Public Sub New(merchantid As String, merchantkey As String, shopid As String)
        Me.merchantId = merchantid
        Me.merchantKey = merchantkey
        Me.shopId = shopid
    End Sub
    Private Sub [error]()
        errorCode = parse("errorcode")
        errorMessage = System.Web.HttpUtility.UrlDecode(parse("errormessage"))
    End Sub
    Private Function parse(search As String) As String
        Return parse(response, search)
    End Function
    Private Function parse(xml As String, search As String) As String
        Dim start As Integer, [end] As Integer
        If (InlineAssignHelper(start, xml.IndexOf("<" & search & ">"))) < 0 Then
            Return Nothing
        End If
        start += search.Length + 2
        If (InlineAssignHelper([end], xml.IndexOf("</" & search & ">", start))) < 0 Then
            Return Nothing
        End If
        Return xml.Substring(start, [end] - start)
    End Function
    Private Function send(method As String, ParamArray keyvalue As String()) As Boolean
        Dim parms As String = ""
        Dim url As String = "https://www.sisow.nl/Sisow/iDeal/RestHandler.ashx/" & method
        Try
            Dim i As Integer = 0
            While i + 1 < keyvalue.Length
                If String.IsNullOrEmpty(keyvalue(i + 1)) Then
                    Continue While
                End If
                If Not String.IsNullOrEmpty(parms) Then
                    parms += "&"
                End If
                parms += keyvalue(i) & "=" & System.Web.HttpUtility.UrlEncode(keyvalue(i + 1))
                i += 2
            End While
            Dim hwr As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create(url), System.Net.HttpWebRequest)
            hwr.ContentType = "application/x-www-form-urlencoded"
            hwr.Method = "POST"
            hwr.ContentLength = parms.Length
            Dim sw As New System.IO.StreamWriter(hwr.GetRequestStream())
            sw.Write(parms)
            sw.Flush()
            sw.Close()
            Dim hws As System.Net.HttpWebResponse = DirectCast(hwr.GetResponse(), System.Net.HttpWebResponse)
            Dim sr As New System.IO.StreamReader(hws.GetResponseStream())
            response = sr.ReadToEnd()
            hws.Close()
            Return True
        Catch generatedExceptionName As Exception
            response = ""
            [error]()
            Return False
        End Try
    End Function
    Private Function getDirectory() As Integer
        If issuerid IsNot Nothing AndAlso lastcheck.AddDays(1).CompareTo(DateTime.Now) >= 0 Then
            Return 0
        End If
        If Not send("DirectoryRequest") Then
            Return -1
        End If
        Dim search As String = parse("directory")
        If String.IsNullOrEmpty(search) Then
            [error]()
            Return -2
        End If
        Dim iss As String() = search.Replace("<issuer>", "").Split(New String() {"</issuer>"}, StringSplitOptions.RemoveEmptyEntries)
        issuerid = New String(iss.Length - 1) {}
        issuername = New String(iss.Length - 1) {}
        For i As Integer = 0 To iss.Length - 1
            issuerid(i) = parse(iss(i), "issuerid")
            issuername(i) = parse(iss(i), "issuername")
        Next
        lastcheck = DateTime.Now
        Return 0
    End Function
    ' DirectoryRequest
    Public Function DirectoryRequest(test As Boolean, ByRef [select] As String) As Integer
        Dim ex As Integer
        [select] = "<select id=""sisowbank"" name=""issuerid"">"
        ex = getDirectory()
        If ex < 0 Then
            Return ex
        End If
        For i As Integer = 0 To issuerid.Length - 1
            [select] += "<option value=""" & issuerid(i) & """>" & issuername(i) & "</option>"
        Next
        [select] += "</select>"
        Return 0
    End Function
    ' DirectoryRequest
    Public Function DirectoryRequest(test As Boolean, ByRef issuers As String()) As Integer
        Dim ex As Integer
        issuers = Nothing
        ex = getDirectory()
        If ex < 0 Then
            Return ex
        End If
        issuers = New String(issuerid.Length * 2 - 1) {}
        For i As Integer = 0 To issuerid.Length - 1
            issuers(i * 2) = issuerid(i)
            issuers(i * 2 + 1) = issuername(i)
        Next
        Return 0
    End Function
    ' compute SHA1
    Private Shared Function GetSHA1(key As String) As String
        Dim sha As New System.Security.Cryptography.SHA1Managed()
        Dim enc As New System.Text.UTF8Encoding()
        Dim bytes As Byte() = sha.ComputeHash(enc.GetBytes(key))
        'string sha1 = System.BitConverter.ToString(sha1).Replace("-", "");
        Dim sha1 As String = ""
        For j As Integer = 0 To bytes.Length - 1
            sha1 += bytes(j).ToString("x2")
        Next
        Return sha1
    End Function
    ' TransactionRequest
    Public Function TransactionRequest() As Integer
        trxId = InlineAssignHelper(issuerUrl, "")
        If String.IsNullOrEmpty(merchantId) Then
            Return -1
        End If
        If String.IsNullOrEmpty(merchantKey) Then
            Return -2
        End If
        If String.IsNullOrEmpty(purchaseId) Then
            Return -3
        End If
        If amount < 0.45 Then
            Return -4
        End If
        If String.IsNullOrEmpty(description) Then
            Return -5
        End If
        If String.IsNullOrEmpty(returnUrl) Then
            Return -6
        End If
        If String.IsNullOrEmpty(issuerId) AndAlso String.IsNullOrEmpty(payment) Then
            Return -7
        End If
        If String.IsNullOrEmpty(entranceCode) Then
            entranceCode = purchaseId
        End If
        Dim sha1 As String = GetSHA1(purchaseId & entranceCode & (amount * 100).ToString() & shopId & merchantId & merchantKey)
        If Not send("TransactionRequest", "shopid", shopId, "merchantid", merchantId, "payment", _
         payment, "issuerid", issuerid, "purchaseid", purchaseId, "amount", _
         (amount * 100).ToString(), "description", description, "entrancecode", entranceCode, "returnurl", _
         returnUrl, "cancelurl", cancelUrl, "callbackurl", callbackUrl, "notifyurl", _
         notifyUrl, "sha1", sha1) Then
            Return -8
        End If
        trxId = parse("trxid")
        If String.IsNullOrEmpty(trxId) Then
            [error]()
            Return -2
        End If
        issuerUrl = System.Web.HttpUtility.UrlDecode(parse("issuerurl"))
        Return 0
    End Function
    Private Function GetStatus() As Integer
        status = parse("status")
        If String.IsNullOrEmpty(status) Then
            [error]()
            Return -5
        End If
        timeStamp = DateTime.Parse(parse("timestamp"))
        amount = Long.Parse(parse("amount")) / 100.0
        consumerAccount = parse("consumeraccount")
        consumerName = parse("consumername")
        consumerCity = parse("consumercity")
        purchaseId = parse("purchaseid")
        description = parse("description")
        entranceCode = parse("entrancecode")
        Return 0
    End Function
    ' StatusRequest
    Public Function StatusRequest() As Integer
        If String.IsNullOrEmpty(merchantId) Then
            Return -1
        End If
        If String.IsNullOrEmpty(merchantKey) Then
            Return -2
        End If
        If String.IsNullOrEmpty(trxId) Then
            Return -3
        End If
        Dim sha1 As String = GetSHA1(trxId & shopId & merchantId & merchantKey)
        If Not send("StatusRequest", "shopid", shopId, "merchantid", merchantId, "trxid", _
         trxId, "sha1", sha1) Then
            Return -4
        End If
        Return GetStatus()
    End Function
    ' StatusRequest
    Public Function StatusRequest(trxid__1 As String) As Integer
        If String.IsNullOrEmpty(merchantId) Then
            Return -1
        End If
        If String.IsNullOrEmpty(merchantKey) Then
            Return -2
        End If
        If String.IsNullOrEmpty(trxid__1) Then
            Return -3
        End If
        trxId = trxid__1
        Dim sha1 As String = GetSHA1(trxId & shopId & merchantId & merchantKey)
        If Not send("StatusRequest", "shopid", shopId, "merchantid", merchantId, "trxid", _
         trxId, "sha1", sha1) Then
            Return -4
        End If
        Return GetStatus()
    End Function
    Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
        target = value
        Return value
    End Function
End Class
End Namespace

将代码从C#转换为VB.NET时出现小错误

您遇到了问题,因为VB.NET不区分大小写,而C#则不区分。

这意味着,比如说,两个变量用类似的名称定义,只是大小写不同——例如issuerIdissuerid——它们在C#中是不同的,但在VB.NET中被认为是相同的。