|
-
Sep 10th, 2006, 12:39 PM
#1
Thread Starter
New Member
[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.
-
Sep 10th, 2006, 02:54 PM
#2
Re: [2005] need help with some vb code
Vartotal is getting "erased" and reassigned a value each time in line
VB Code:
vartotal = varval * varmulti
To fix this (and if THIS is the problem), put a slightly modified lblResult.Text assignment inside the loop:
VB Code:
lblResult.Text += CStr(vartotal) & vbCrLf
You should probably use a string for that though and put the whole in the label after the loop.
-
Sep 10th, 2006, 02:59 PM
#3
Re: [2005] need help with some vb code
Here is how you convert
VB Code:
Public Function ToBinary(ByVal Number As Integer) As String
Dim strDecimalNumber As String = ""
Dim strReturnValue As String = ""
Dim strBinValue As String = ""
Dim lngStringLength As Integer
Dim lngIndex As Integer
strDecimalNumber = Oct(Number)
lngStringLength = Len(strDecimalNumber)
For lngIndex = 1 To lngStringLength
Select Case Integer.Parse(Mid$(strDecimalNumber, lngIndex, 1))
Case 0
strBinValue = "000"
Case 1
strBinValue = "001"
Case 2
strBinValue = "010"
Case 3
strBinValue = "011"
Case 4
strBinValue = "100"
Case 5
strBinValue = "101"
Case 6
strBinValue = "110"
Case 7
strBinValue = "111"
End Select
strReturnValue &= strBinValue
Next
ToBinary = strReturnValue
End Function
Public Function ToInteger(ByVal Binary As String) As Integer
Return Convert.ToInt32(Binary, 2)
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
-
Sep 10th, 2006, 03:01 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|