hi. so i need help in understanding this program.
its a hindu arabic - roman numeral converter.
i got it from some site.
i cant seem to understand how the whole process goes.

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

here are my few questions about the program.
1. for instance. my input is 2345 to be converted to roman numeral which should be. MMCCCXLV
as we go over to the to roman function on the if statement :
If Len(sDecNum) > 3 Then text2.Text = String(Mid(sDecNum, 1, Len(sDecNum) - 3), "M")
it falls true here. so im not exactly sure how this whole line of code works. all i know is that this line should be able to display MM on text2.text.

  1. as with this line of code :
    If Len(sDecNum) > 2 Then text2.Text = text2.Text & GiveLetters(Mid(sDecNum, Len(sDecNum) - 2, 1), 4)
    my inputs falls true on this again. but i cant seem to understand this. dont know what values are returned
    but what im was thinking was this : GiveLetters(Mid(sDecNum, 3, 1), 4.

  2. lastly, im dont know when will an sinput fall on a case on my giveletter function.
    i cant seem to understand how this function works.

thaaank you so much for the help!

Recommended Answers

All 5 Replies

Option Explicit

Const sMatrix As String = "I~V~X~L~C~D~M"
''Holder of numerical converted value...

Private Function toroman(ByVal sDecNum As String) As String

Text1.Text = sDecNum
''Value to be converted from numerical to roman...

If sDecNum <> "0" And sDecNum <> vbNullString Then ''Check to see if numerical value is valid...
    If Len(sDecNum) > 3 Then Text2.Text = String(Mid(sDecNum, 1, Len(sDecNum) - 3), "M")
    ''If text1's length is longer than 3 characters (1234 etc) ...
    ''then take the string (1234)...
    ''Mid = Returns a substring containing a specified number of characters from a string...
    ''Read more about the string functions at - http://www.vb6.us/tutorials/vb6-string-functions...

    If Len(sDecNum) > 2 Then Text2.Text = Text2.Text & GiveLetters(Mid(sDecNum, Len(sDecNum) - 2, 1), 4)
    ''Same as above...

    If Len(sDecNum) > 1 Then Text2.Text = Text2.Text & GiveLetters(Mid(sDecNum, Len(sDecNum) - 1, 1), 2)
    ''Same...
            Text2.Text = Text2.Text & GiveLetters(Mid(sDecNum, Len(sDecNum), 1), 0)
            ''Call Give Letters function...
    Else: Text2.Text = "No Roman value for 0"
    ''No value in text1...
End If
End Function
Private Function GiveLetters(ByVal sInput As String, ByVal iArrStart As Integer) As String

Dim sLetterArray() As String
sLetterArray() = Split(sMatrix, "~")
''Will return one of the values declared in sMatrix where the split will occur at the ~ sign...

Select Case sInput
    Case 4: GiveLetters = sLetterArray(iArrStart) & sLetterArray(iArrStart + 1)
    ''If sInput has a value of "4" it will return value from sMatrix = IV ... Same wih 5 etc..
    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

As above, read more on string functions [Here](http://www.vb6.us/tutorials/vb6-string-functions). Once you get the understanding of how a value is returned by using split, mid etc, you'll grasp the conversion easily...

thaak you so much for the response. but i already understand what some of the function here does.
well, a bit. maybe. if it isnt much, can you do a simple simulation or something?
perhaps a simulation on a 4 digt input?

thaaaaaank you so much. appreciate the response.

If you say simulation, do you mean get teh code to do something? Easy, paste the code into a form, add 2 textboxes and a command button. Change the text in text1 to say 1932.

thaak you som much for the response. but i already understand what this function does.
well, a bit. maybe. if it isnt much, can you do a simple simulation or something?
perhaps a simulation on a 4 digt input?

thaaaaaank you so much. appreciate the response.

no. i mean. more like a walk through on how the whole program works.
like for me to actually know what values are returned each time a line of code is executed.
more like a "textual test run" on the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.