2014-04-17 112 views
0

我正嘗試在VB.net中創建自定義的西里爾文轉換爲拉丁文本函數。我從來沒有試過做一個自定義函數,所以我不知道我做錯了什麼。我有一個問題,而且,函數不起作用:對象引用未設置爲對象的實例。函數將西里爾文轉換爲拉丁文

Public Function ConvertCtoL(ByVal ctol As String) As String 

    ctol = Replace(ctol, "Б", "B") 
    ctol = Replace(ctol, "б", "b") 

**End Function** ' doesn't return a value on all code paths 

由於我沒有發現西里爾到拉丁文本我正打算創建功能將取代每個字母從一個字母到另一個解決方案。

回答

1

您需要Return ctol來告訴它返回什麼值。

也許研究「查找表」可以幫助你做出更好的功能。

編輯:維基百科條目Lookup table應該是一個好的開始。

下面是一個簡單的例子:

Imports System.Text 

Module Module1 

    Function ReverseAlphabet(s As String) As String 
     Dim inputTable() As Char = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray() 
     Dim outputTable() As Char = "ZYXWVUTSRQPONMLKJIHGFEDBCA".ToCharArray() 
     Dim sb As New StringBuilder 

     For Each c As Char In s 
      Dim inputIndex = Array.IndexOf(inputTable, c) 
      If inputIndex >= 0 Then 
       ' we found it - look up the value to convert it to. 
       Dim outputChar = outputTable(inputIndex) 
       sb.Append(outputChar) 
      Else 
       ' we don't know what to do with it, so leave it as is. 
       sb.Append(c) 
      End If 
     Next 

     Return sb.ToString() 

    End Function 

    Sub Main() 
     Console.WriteLine(ReverseAlphabet("ABC4")) ' outputs "ZYX4" 
     Console.ReadLine() 
    End Sub 

End Module 
+0

謝謝你,你可以給有關查找表中的一些鏈接,因爲我不知道的事情。 – Jovica

+1

@ user3338345我已經使用鏈接和簡單示例編輯了我的答案。 –

相關問題