soooo. i saw a working code for a hindu arabic - roman numeral converter. the thing is. i need
to fully understand the code.
so i need someone who could explain the code clearly to me. line by line as for me to fully
understand the program. newbie here.

you can add comments or just post a reply.
thaanks.

Const sMatrix As String = "I~V~X~L~C~D~M"
Private Function toroman(ByVal sDecNum As String) As String
    Text1.Text = sDecNum
    If sDecNum <> "0" And sDecNum <> vbNullString Then
        If Len(sDecNum) > 3 Then text2.Text = String(Mid(sDecNum, 1, Len(sDecNum) - 3), "M")
        If Len(sDecNum) > 2 Then text2.Text = text2.Text & GiveLetters(Mid(sDecNum, Len(sDecNum) - 2, 1), 4)
        If Len(sDecNum) > 1 Then text2.Text = text2.Text & GiveLetters(Mid(sDecNum, Len(sDecNum) - 1, 1), 2)
            text2.Text = text2.Text & GiveLetters(Mid(sDecNum, Len(sDecNum), 1), 0)
    Else: text2.Text = "No Roman value for 0"
    End If
End Function

Private Function GiveLetters(ByVal sInput As String, ByVal iArrStart As Integer) As String

    Dim sLetterArray() As String
    sLetterArray() = Split(sMatrix, "~")

    Select Case sInput
        Case 4: GiveLetters = sLetterArray(iArrStart) & sLetterArray(iArrStart + 1)
        Case 5: GiveLetters = sLetterArray(iArrStart + 1)
        Case 9: GiveLetters = sLetterArray(iArrStart) & sLetterArray(iArrStart + 2)
        Case 6 To 8: GiveLetters = sLetterArray(iArrStart + 1) & String(sInput - 5, sLetterArray(iArrStart))
        Case Else: GiveLetters = GiveLetters + String(sInput, sLetterArray(iArrStart))
    End Select
End Function

Private Sub Command1_Click()
    Dim sRoman As String

        If Val(Text1.Text) < 1 Or Val(Text1.Text) > 3999 Then
            MsgBox "Invalid Input. Please type in an integer from 0 - 3999 only!", _
                   vbExclamation

            Text1.SetFocus
            Text1.Text = ""
            Exit Sub
         Else

        sRoman = toroman(Text1.Text)
    End If
End Sub

No one is going to explain this line by line. Make an effort yourself and ask specific questions about the portions that you are unclear on.

ok. so ill make it more specific. ill italicize the lines of codes i need help with. is that ok now?

Const sMatrix As String = "I~V~X~L~C~D~M"
Private Function toroman(ByVal sDecNum As String) As String
Text1.Text = sDecNum
If sDecNum <> "0" And sDecNum <> vbNullString Then
If Len(sDecNum) > 3 Then text2.Text = String(Mid(sDecNum, 1, Len(sDecNum) - 3), "M")
If Len(sDecNum) > 2 Then text2.Text = text2.Text & GiveLetters(Mid(sDecNum, Len(sDecNum) - 2, 1), 4)*
If Len(sDecNum) > 1 Then text2.Text = text2.Text & GiveLetters(Mid(sDecNum, Len(sDecNum) - 1, 1), 2)
text2.Text = text2.Text & GiveLetters(Mid(sDecNum, Len(sDecNum), 1), 0)
Else: text2.Text = "No Roman value for 0"
End If
End Function
Private Function GiveLetters(ByVal sInput As String, ByVal iArrStart As Integer) As String
Dim sLetterArray() As String
sLetterArray() = Split(sMatrix, "~")
Select Case sInput
Case 4: GiveLetters = sLetterArray(iArrStart) & sLetterArray(iArrStart + 1)
Case 5: GiveLetters = sLetterArray(iArrStart + 1)
Case 9: GiveLetters = sLetterArray(iArrStart) & sLetterArray(iArrStart + 2)
Case 6 To 8: GiveLetters = sLetterArray(iArrStart + 1) & String(sInput - 5, sLetterArray(iArrStart))
Case Else: GiveLetters = GiveLetters + String(sInput, sLetterArray(iArrStart))
End Select
End Function
Private Sub Command1_Click()
Dim sRoman As String
If Val(Text1.Text) < 1 Or Val(Text1.Text) > 3999 Then
MsgBox "Invalid Input. Please type in an integer from 0 - 3999 only!", _
vbExclamation
Text1.SetFocus
Text1.Text = ""
Exit Sub
Else
sRoman = toroman(Text1.Text)
End If
End Sub

Edited 4 Years Ago by yeeitsneo

This question has already been answered. Start a new discussion instead.