簡體   English   中英

.net-由於增量運算符或自動字符串轉換而無法將代碼從C#轉換為vb.net(我認為)

[英].net - unable to convert code from c# to vb.net due to incremental operator or automatic string conversion (I think)

我正在VB.NET中編寫一個庫,其中添加了一個最初用C#編寫但轉換為VB.NET的類。 我對C#不太了解,因此我在VB.NET轉換器上使用了在線C#。 現在,我陷入了一些代碼,我相信在線轉換器無法正確地“翻譯”。

運行代碼時,出現以下錯誤:

System.IndexOutOfRangeException was unhandled
  Message="IndexOutOfRangeException"
  StackTrace:
    at System.String.get_Chars()
.........

我真的不明白此錯誤的原因。 我認為此錯誤可能是由於以下原因造成的:-由於C#能夠將整數變量自動轉換為字符串(而VB.NET需要“ toString方法”)-或由於C#使用增量式VB.NET不支持的運算符。

這是C#中的原始代碼片段,其中“ m_primaryKey”是StringBuilder對象:

private void addMetaphoneCharacter(String primaryCharacter, String alternateCharacter)
        {
            //Is the primary character valid?
            if (primaryCharacter.Length > 0)
            {
                int idx = 0;
                while (idx < primaryCharacter.Length)
                {
                    m_primaryKey.Length++;
                    m_primaryKey[m_primaryKeyLength++] = primaryCharacter[idx++];
                }
            }
            //other code deleted

使用C#中創建的類庫時,此原始代碼有效。

這是VB.NET中轉換后的代碼,它給了我前面提到的錯誤:

Private Sub addMetaphoneCharacter(ByVal primaryCharacter As String, ByVal alternateCharacter As String)
            'Is the primary character valid? 
            If primaryCharacter.Length > 0 Then
                Dim idx As Integer = 0
                While idx < primaryCharacter.Length
                    m_primaryKey.Length += 1
                    m_primaryKey(System.Math.Max(System.Threading.Interlocked.Increment(m_primaryKeyLength), _
                        m_primaryKeyLength - 1)) = primaryCharacter _
                        (System.Math.Max(System.Threading.Interlocked.Increment(idx), idx - 1))
                End While
            End If 
           'other code deleted  

原始代碼可以在這里找到

我應該說該類中的代碼對我來說是相當高級的(我是一個愛好程序員,但是每天都要學習),所以也許我看不到明顯的東西,但這就是我問你的原因。

您能給我任何提示以解決這個問題嗎?

謝謝。

編輯:這是C#中的完整子集:

/**
         * Appends a metaphone character to the primary, and a possibly different alternate,
         * metaphone keys for the word.
         * 
         * @param primaryCharacter
         *               Primary character to append to primary key, and, if no alternate char is present,
         *               the alternate key as well
         * @param alternateCharacter
         *               Alternate character to append to alternate key.  May be null or a zero-length string,
         *               in which case the primary character will be appended to the alternate key instead
         */
        private void addMetaphoneCharacter(String primaryCharacter, String alternateCharacter)
        {
            //Is the primary character valid?
            if (primaryCharacter.Length > 0)
            {
                int idx = 0;
                while (idx < primaryCharacter.Length)
                {
                    m_primaryKey.Length++;
                    m_primaryKey[m_primaryKeyLength++] = primaryCharacter[idx++];
                }
            }

            //Is the alternate character valid?
            if (alternateCharacter != null)
            {
                //Alternate character was provided.  If it is not zero-length, append it, else
                //append the primary string as long as it wasn't zero length and isn't a space character
                if (alternateCharacter.Length > 0)
                {
                    m_hasAlternate = true;
                    if (alternateCharacter[0] != ' ')
                    {
                        int idx = 0;
                        while (idx < alternateCharacter.Length)
                        {
                            m_alternateKey.Length++;
                            m_alternateKey[m_alternateKeyLength++] = alternateCharacter[idx++];
                        }
                    }
                }
                else
                {
                    //No, but if the primary character is valid, add that instead
                    if (primaryCharacter.Length > 0 && (primaryCharacter[0] != ' '))
                    {
                        int idx = 0;
                        while (idx < primaryCharacter.Length)
                        {
                            m_alternateKey.Length++;
                            m_alternateKey[m_alternateKeyLength++] = primaryCharacter[idx++];
                        }
                    }
                }
            }
            else if (primaryCharacter.Length > 0)
            {
                //Else, no alternate character was passed, but a primary was, so append the primary character to the alternate key
                int idx = 0;
                while (idx < primaryCharacter.Length)
                {
                    m_alternateKey.Length++;
                    m_alternateKey[m_alternateKeyLength++] = primaryCharacter[idx++];
                }
            }
        }

在VB.NET版本中,while循環第二行的參數完全不同。 Interlocked.Increment調用將返回增量索引,而C#后增量運算符將返回原始值(增量之前)。

第二行可能會更好地替換為:

m_primaryKey(m_primaryKeyLength) = primaryCharacter(idx)
m_primaryKeyLength = m_primaryKeyLength + 1
idx = idx + 1

也就是說,按照C#原始規范,僅在執行索引/分配后才遞增值。

我可能會丟失一些內容,但是我看不到為何轉​​換器開始使用System.Threading.Interlocked的相關性。

我的手動轉換如下所示:

Private Sub addMetaphoneCharacter(ByVal primaryCharacter As String, ByVal alternateCharacter As String)
            'Is the primary character valid? 
            If primaryCharacter.Length > 0 Then
                Dim idx As Integer = 0
                While idx < primaryCharacter.Length
                    m_primaryKey.Length += 1
                    m_primaryKey(m_primaryKeyLength) = primaryCharacter(idx)
                    m_primaryKeyLength += 1
                    idx += 1

                End While
            End If 
           'other code deleted

索引越界意味着您正在嘗試訪問數組大小之外的內容。 這通常是由“合一”問題引起的。

看一下代碼,VB轉換很奇怪,並且正在做C#版本沒有的事情,例如從索引器中刪除一個。

您是在說自動化工具嗎?

順便說一句,您可以毫無問題地將C#類和程序集包含在VB應用程序中,那么為什么要轉換它呢?

將Math.Max更改為Math.Min應該可以解決此問題,但是為清楚起見,我將按照itowlson所說的那樣進行更改,並在使用增量之后將其移動到。

while循環中的兩行代碼在C#中執行以下操作:-將m_primaryKey.Length遞增1-將m_primaryKeyLength遞增1(您確定此處沒有遺漏點嗎?)-將idx遞增1-分配值primaryCharacter [idx]到m_primaryKey [m_primaryKeyLength]

所以在VB代碼中...

m_primaryKey.Length += 1
m_primaryKeyLength += 1
idx += 1

m_primaryKey(m_primaryKeyLength) = primaryCharacter (idx)

我無法從此代碼片段看出來,但聞起來像m_primaryKeyLength和m_primaryKey.Length是多余的。 在這種情況下,通過將“ m_primaryKeyLength”替換為“ m_primaryKey.Length”來簡化代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM