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

由于我没有找到将西里尔文转换为拉丁文的解决方案,因此我计划创建一个函数,将每个字母从一个字母表替换为另一个字母表。

4

1 回答 1

1

你需要Return ctol告诉它返回什么值。

也许研究“查找表”会帮助您制作更简洁的功能。

编辑:查找表的维基百科条目应该是一个好的开始。

这是一个简单的例子:

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
于 2014-04-18T16:19:30.487 回答