Question

I am trying to create custom convert cyrillic to latin text function in VB.net. I've never tried to make a custom function so I don't know what I am doing wrong. I have one problem and also, function doesn't work : Object reference not set to an instance of an object.

    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

Since I didn't found a solution for cyrillic to latin text I was planning to create function that would replace every letter from one alphabet to another.

Was it helpful?

Solution

You need Return ctol to tell it what value to return.

Perhaps researching "lookup table" would help you make a neater function.

Edit: The Wikipedia entry for Lookup table should be a good start.

Here is a simple example:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top