Results 1 to 4 of 4

Thread: [2005] need help with some vb code

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    2

    Question [2005] need help with some vb code

    hello, i am a student and a newbie to visual basic and i have been set some homework, i have to create a binary converter and i have got quite far but for some reason it is not working right, i believe that the loop in my code is no adding up the vartotals every time it loops, the code i have made is this:

    Public Class FfmBinConv

    Private Sub cmdok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdok.Click

    Dim vartotal, varmulti, varcontrol As Integer

    Dim varright, varval, varbin As String

    varbin = txtBin.Text

    varmulti = 1

    For varcontrol = 8 To 1 Step -1

    varright = Mid(varbin, varcontrol, 1)

    varval = CInt(varright)

    vartotal = varval * varmulti

    varmulti = varmulti * 2

    Next varcontrol

    lblResult.Text = CStr(vartotal)


    End Sub
    End Class

    Any help would be very much appreciated.

  2. #2
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [2005] need help with some vb code

    Vartotal is getting "erased" and reassigned a value each time in line
    VB Code:
    1. vartotal = varval * varmulti
    To fix this (and if THIS is the problem), put a slightly modified lblResult.Text assignment inside the loop:
    VB Code:
    1. lblResult.Text += CStr(vartotal) & vbCrLf
    You should probably use a string for that though and put the whole in the label after the loop.
    VB 2005, Win Xp Pro sp2

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [2005] need help with some vb code

    Here is how you convert
    VB Code:
    1. Public Function ToBinary(ByVal Number As Integer) As String
    2.         Dim strDecimalNumber As String = ""
    3.         Dim strReturnValue As String = ""
    4.         Dim strBinValue As String = ""
    5.         Dim lngStringLength As Integer
    6.         Dim lngIndex As Integer
    7.         strDecimalNumber = Oct(Number)
    8.         lngStringLength = Len(strDecimalNumber)
    9.         For lngIndex = 1 To lngStringLength
    10.             Select Case Integer.Parse(Mid$(strDecimalNumber, lngIndex, 1))
    11.                 Case 0
    12.                     strBinValue = "000"
    13.                 Case 1
    14.                     strBinValue = "001"
    15.                 Case 2
    16.                     strBinValue = "010"
    17.                 Case 3
    18.                     strBinValue = "011"
    19.                 Case 4
    20.                     strBinValue = "100"
    21.                 Case 5
    22.                     strBinValue = "101"
    23.                 Case 6
    24.                     strBinValue = "110"
    25.                 Case 7
    26.                     strBinValue = "111"
    27.             End Select
    28.             strReturnValue &= strBinValue
    29.         Next
    30.         ToBinary = strReturnValue
    31.     End Function
    32.     Public Function ToInteger(ByVal Binary As String) As Integer
    33.         Return Convert.ToInt32(Binary, 2)
    34.     End Function
    If you have any problems understanding, feel free to ask
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [2005] need help with some vb code

    And by the way, to the forums
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width