Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 1.0 and Visual Studio.NET > VS.NET 2002/2003
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 4th, 2004, 07:24 AM
dib dib is offline
Registered User
 
Join Date: Feb 2004
Location: Darwin, NT, Australia.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default newbie vb.net question

Gday Folks - been reading your starting vb.net book and love it - still lots to learn however. I have tried to make a little application that converts between binary, oct, decimal and hex numbers. It works ok but if I type in too many numbers I get an overflow error - and the amount I type in I wouldn't expect this.

Following is my code so far - its not real flash and I haven't created any classes or objects. Any help much appreciated.

Public Sub UpdateNumberFields(ByVal DecValue As Decimal, ByVal Base As Int16)
        If Base <> 2 Then Me.txtBinary.Text = ConvertToBaseNumber(DecValue, 2)
        If Base <> 8 Then Me.txtOctimal.Text = ConvertToBaseNumber(DecValue, 8)
        If Base <> 10 Then Me.txtDecimal.Text = ConvertToBaseNumber(DecValue, 10)
        If Base <> 16 Then Me.txtHexadecimal.Text = ConvertToBaseNumber(DecValue, 16)
    End Sub

    Private Sub txtBinary_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBinary.TextChanged
        If Me.txtBinary.ContainsFocus = True And Me.txtBinary.Text <> "" Then
            UpdateNumberFields(ConvertToDecimal(Me.txtBinary.T ext, 2), 2)
        End If
    End Sub

    Private Sub txtOctimal_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtOctimal.TextChanged
        If Me.txtOctimal.ContainsFocus = True And Me.txtOctimal.Text <> "" Then
            UpdateNumberFields(ConvertToDecimal(Me.txtOctimal. Text, 8), 8)
        End If
    End Sub


    Private Sub txtHexadecimal_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtHexadecimal.TextChanged
        If Me.txtHexadecimal.ContainsFocus = True And Me.txtHexadecimal.Text <> "" Then
            UpdateNumberFields(ConvertToDecimal(Me.txtHexadeci mal.Text, 16), 16)
        End If
    End Sub

    Private Sub txtDecimal_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDecimal.TextChanged

        If Me.txtDecimal.ContainsFocus = True And Me.txtDecimal.Text <> "" Then
            UpdateNumberFields(Me.txtDecimal.Text, 10)
        End If

    End Sub


    'Convert a decimal to another base
    Public Function ConvertToBaseNumber(ByVal passedValue As Decimal, ByVal base As Int16) As String

        Dim Counter, Length As Int16
        Dim PosValue, DigitValue As Decimal
        Dim Result As String

        'Step thru each character
        Length = FindFirstDigitPos(passedValue, base)
        For Counter = Length To 0 Step -1
            'calculate the character position value
            PosValue = base ^ Counter
            'calculate the value of current digit ignoring position (eg the F in 0xFD8E = 15)
            DigitValue = Math.Floor(passedValue / PosValue)
            'leave in passedValue remainder to be calculated
            passedValue = passedValue - (DigitValue * PosValue)
            'if 0-9 just add digit to result string
            If DigitValue >= 0 And DigitValue <= 9 Then
                Result = Result & DigitValue
            Else
                'if more than 9 add corresponding alpha to text string (eg 10 = A, 11 = B etc)
                Result = Result & Chr(DigitValue + 55)
            End If

        Next
        Return Result
    End Function

    Public Function FindFirstDigitPos(ByVal foobar As Long, ByVal base As Int16) As Int16
        Dim Counter, PosValue As Integer
        Do
            PosValue = base ^ Counter
            Counter = Counter + 1
            'Do until the value of the position from the right is more than half
        Loop While PosValue < foobar / 2
        Return Counter
    End Function

    'take a 2, 8 or 16 base number and return decimal equivalent...
    Public Function ConvertToDecimal(ByVal passedVal As String, ByVal Base As Integer) As Decimal

        Dim Counter As Integer
        Dim DecValue As Decimal
        Dim Ubound As Integer = passedVal.Length
        'if binary or octimal...
        If Base = 2 Or Base = 8 Then

            'step through each character in number string...

            For Counter = 1 To Ubound
                Dim digit = passedVal.Substring(Ubound - Counter, 1)
                If digit <> "0" Then
                    'check digit is legal for number base (eg not over 1 if binary and not over 7 if oct...
                    If digit > Base - 1 Then MessageBox.Show("Alert - digit can't be greater than " & Base - 1)
                    'add to decimal result value of digit calculated against base and position....
                    DecValue = DecValue + (Base ^ (Counter - 1) * digit)
                End If
            Next
            Return DecValue
            'if hexadecimal...
        ElseIf Base = 16 Then
            'convert to Uppercase...
            passedVal = UCase(passedVal)

            'step through each character
            For Counter = 1 To Ubound
                Dim digit As String = passedVal.Substring(Ubound - Counter, 1)
                If digit <> "0" Then

                    'if character a number (0-9)?
                    If Asc(digit) >= 48 And Asc(digit) <= 57 Then
                        'add character * Base ^ Position
                        DecValue = DecValue + (Base ^ (Counter - 1) * Val(digit))

                        'or is character A-F?
                    ElseIf Asc(digit) >= 65 And Asc(digit) <= 70 Then
                        'add calculated value in hex
                        DecValue = DecValue + (16 ^ (Counter - 1) * (Asc(digit) - 55))
                        'or if illegal report error
                    Else
                        MessageBox.Show("Alert: Illegal Character")
                    End If
                End If

            Next
            Return DecValue
        End If
    End Function
Reply With Quote
  #2 (permalink)  
Old February 4th, 2004, 11:27 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Which part of this code has the error? Do you have a utility that debugs this code, such as VS.NET?
Reply With Quote
  #3 (permalink)  
Old February 5th, 2004, 04:19 AM
dib dib is offline
Registered User
 
Join Date: Feb 2004
Location: Darwin, NT, Australia.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The problem seems to be throughout it but I don't know where. I have four text fields on the form but when they get to say 9 or so digits I get an overflow error. Works fine otherwise.

I do have VS.NET but when I go to insert a break point I get an error stating "The break point will not currently be hit. No symbols have been loaded for this document" so I can't seem to debug it.
Reply With Quote
  #4 (permalink)  
Old February 5th, 2004, 12:32 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

When debuggin ASP.net you'll often see this error. The breakpoint is set but it claims it won't be hit. Typically this is because there is no code running at the time. Once you actually are in the cycle of a page request, the breakpoints should work normally. This is "normal" behavior.

Peter
------------------------------------------------------
Work smarter, not harder.
Reply With Quote
  #5 (permalink)  
Old February 5th, 2004, 04:46 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Maybe try changing some data types to a larger datatype, such as long, that are integer. This may be causing your problem, but I'm not sure.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DTS VB ActiveX Embarrassing newbie question. Jambaroo SQL Server DTS 2 April 12th, 2007 03:00 AM
Newbie .Net Question fs22 General .NET 0 May 4th, 2006 10:17 AM
Good book for VB.NET newbie Dhodie VB.NET 2002/2003 Basics 4 April 7th, 2006 04:32 AM
Newbie: Calling a VB doc from ASP.NET kwilliams Classic ASP Basics 2 August 31st, 2005 09:10 AM
VB.Net question on Windows VB.Net datagrids dmsousa VS.NET 2002/2003 1 January 19th, 2005 02:45 PM



All times are GMT -4. The time now is 01:55 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.