dcsimg
Results 1 to 13 of 13
  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    3

    Break up numbers and loop.

    I need some help looping the Break Up numbers because right now it is just at 100 and I need it to go to 125. Everything else works fine but I feel like I am missing something simple.

    Sorry for the messy code. I am a student and I am kinda getting the hang of this.

    I added a picture of what the application does once I press the button.

    Thanks, Jacob

    Code:
    Public Class Form1
        Dim onesDigit As Integer
        Dim tensDigit As Integer
        Dim hundredsDigit As Integer
        Private Sub btnBreak_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBreak.Click
    
            lblNumber.Text = Build()
            lblRebuild.Text = Build()
            lblBreakUp.Text = BreakUp()
    
        End Sub
    
        Sub TwoDigits(ByVal num As Integer, ByRef firstDigit As Integer, ByRef secondDigit As Integer)
            firstDigit = num / 10
            secondDigit = num Mod 10
        End Sub
    
        Sub ThreeDigits(ByVal num As Integer, ByRef firstDigit As Integer, ByRef secondDigit As Integer, ByRef thirdDigit As Integer)
            firstDigit = num / 100
            num = num Mod 100
            Call TwoDigits(num, secondDigit, thirdDigit)
        End Sub
    
        Function Build()
            Dim starting As Integer = 100
            Dim ending As Integer = 125
            Dim result As String
            For numbers = starting To ending Step 1
                If numbers > starting Then
                    result = result & vbCrLf & numbers
                End If
                If numbers = starting Then
                    result = result & numbers
                End If
            Next
    
            Return result & vbNewLine
        End Function
    
        'The breakup does not work at this point in time.
        Function BreakUp()
            Dim starting As Integer = 100
            Dim ending As Integer = 125
            Dim result As String = 100
    
            'For numbers As Integer = 100 To 125 Step 1
            'Call ThreeDigits(numbers, hundredsDigit, tensDigit, onesDigit)
            'Return hundredsDigit & Space(5) & tensDigit & Space(5) & onesDigit & vbNewLine & numbers
            'Next numbers
    
            For numbers = starting To ending Step 1
                Call ThreeDigits(result, hundredsDigit, tensDigit, onesDigit)
                If numbers > starting Then
                    result = numbers & hundredsDigit & Space(5) & tensDigit & Space(5) & onesDigit & vbNewLine
    
                End If
    
                If numbers = starting Then
                    result = result & numbers
                End If
    
                Return hundredsDigit & Space(5) & tensDigit & Space(5) & onesDigit & vbNewLine
            Next numbers
    
        End Function
       
    End Class
    Attached Images Attached Images  

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,497

    Re: Break up numbers and loop.

    You posted in the VB6 forum.
    This is VB.Net code and should have been posted in the VB.Net forum.
    I'll ask a moderator to move it.

    p.s. I took a look at the code.
    It looks like you're trying to use the build function as a template for the breakup function, but you've made several implementation errors.
    Rather than just point out the various errors, I thing you should try using the debugging capabilities of the IDE as that is a very useful skill to learn early in your study.
    You can step through the code one line at a time, examine the values of the variables and watch the path the code takes through your logical branches (if conditions and loops).

    Since your breakup function is based on your build function, you can step through build function to see how it loops, versus how your breakup function loops.
    Also look at how you build up the string in the build function compared to how you build up the string in your breakup function.
    Look at what arguments you pass to your subs in the build function compared to the arguments you pass in the breakup function.

    Just do a quick web search using the phrase, vb.net debug tutorial, and check out a youtube video or MSDN article or several other links. Multiple sources of a topic are usually good to study so you get a broader perspective, and perhaps filter out some dubious advice if the approaches differ from the various sources.

    Using the built-in debugger should actually be taught on Day One of any VB.Net course, and probably other languages as well.
    Stepping through the code in the debugger while describing some of the initial concepts of the programming language would help in understanding some of those basic concepts, I think, while also getting you familiar with one of the best tools you will ever use as part of your programming experience.

    If you use the debugger and step through the code and still have questions, then ask a specific question about the step where you see it not do what you expect. My third paragraph above actually hints at several of the issues that you should find if you step through the code, so if you find a specific failure and can't figure out what the fix should be (the working Build function should help in determining what the fix should be), we will be glad to assist.
    Last edited by passel; Dec 14th, 2017 at 04:26 AM.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,933

    Re: Break up numbers and loop.

    Welcome to VBForums

    Thread moved from the 'VB6 and Earlier' forum to the 'VB.Net' (VB2002 and later) forum


    Here is some good debugging information: https://msdn.microsoft.com/en-us/library/y740d9d3.aspx

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    3

    Re: Break up numbers and loop.

    Thank you for replying, You have tough me more then my teacher ever has. I did what you said and now it is looping but the looping is not right. It is going like this. "100 0 0" all the way to 125. I think it has to do with were I put my "Call ThreeDigits(result, hundredsDigit, tensDigit, onesDigit)" and if I put it into the loop I get the error "Conversion from string "100 0 0
    101" to type 'Integer' is not valid." So that and some other things I do not realize creating problems. Here is what I got for the breakup now. If you can hint me in the right direction it would be greatly appreciated.

    Code:
        Function BreakUp()
            Dim starting As Integer = 100
            Dim ending As Integer = 125
            Dim result As String
    
            For numbers = starting To ending Step 1
                Call ThreeDigits(result, hundredsDigit, tensDigit, onesDigit)
                If numbers > starting Then
                    result = result & Space(5) & tensDigit & Space(5) & onesDigit & vbNewLine & numbers
    
                End If
    
                If numbers = starting Then
                    result = result & numbers
                End If
    
            Next
    
            Return result & Space(5) & tensDigit & Space(5) & onesDigit & vbNewLine
        End Function

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    39,933

    Re: Break up numbers and loop.

    Take a look at this:
    Code:
            Dim result As String
                ...
                Call ThreeDigits(result, hundredsDigit, tensDigit, onesDigit)
    and how that sub is set up:
    Code:
        Sub ThreeDigits(ByVal num As Integer, ByRef firstDigit As Integer, ByRef secondDigit As Integer, ByRef thirdDigit As Integer)
    I have a feeling that you didn't want to use the variable result there

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,188

    Re: Break up numbers and loop.

    There isn't any real need to use the word Call....ever. Some people like to, and it does no harm, but it's really just a relic of olden days. It doesn't do anything anymore.

    One other thing that you will want to do, if you are allowed, is to turn Option Strict ON. This can be done globally in Options, or can be done for the project in the Project | Properties page. Option Strict just disallows implicit conversions. Implicit conversions allows you to be a bit sloppy with converting one type to another, which can make some code a bit easier to write, but can also hide bugs. In this case, for example, Option Strict would have given you a compile time error rather than a (much less clear) runtime error. Result is a string, but the first argument has to be an Integer. With Option Strict OFF, the compiler will quietly attempt to convert the string to an integer. If that works, all is well enough, though implicit conversions are slightly slower than explicit conversions (you won't see the difference unless you do tens of thousands of them in a row). However, if result has a string that can't be converted, then you get the very error you are seeing. With Option Strict ON, the compiler would tell you right away that you are passing in a string where an integer is expected.
    My usual boring signature: Nothing

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,497

    Re: Break up numbers and loop.

    si_the_geek gave you one hint.
    You're passing "result", but since you want to breakup "numbers" you should be passing the variable numbers to the ThreeDigits sub, not result.
    I would have liked you to figure that out for yourself, but it can be subtle for a newbie, and since you don't call a function in the Build sub, you don't have a direct example of what should be done.

    In the build sub, you don't initialize "result", so you shouldn't be initializing it in the BreakUp routine either (and not pass it to the sub).

    In the Build sub, you have the line:

    result = result & vbCrLf & numbers

    Note that the vbCrLf comes before you append "numbers".
    In your break up routing, you should be doing the same thing.

    In your first post you had this:
    result = numbers & hundredsDigit & Space(5) & tensDigit & Space(5) & onesDigit & vbNewLine

    In post 4, you changed it to this:
    result = result & Space(5) & tensDigit & Space(5) & onesDigit & vbNewLine & numbers

    You don't want to append numbers to the end of that line, since your desired was to break numbers up into hundreds tens and ones.
    You should go back to your original line, but move the NewLine to the front so it matches the order that you used in Build:
    result = result & vbNewLine & hundredsDigit & Space(5) & tensDigit & Space(5) & onesDigit

    You can actually shorten that a bit by using "operator=", so you don't have to include the result string on the right side:
    result &= vbNewLine & hundredsDigit & Space(5) & tensDigit & Space(5) & onesDigit

    Another option is to use string formating, but in this case, it is actually a longer statement. Don't know if it is quicker than using the & operator multiple times.
    result &= String.Format("{0}{2}{1}{3}{1}{4}", vbNewLine, Space(5), hundredsDigit, tensDigit, onesDigit)

    At the end of the Build Sub you have the line
    Return result & vbNewLine

    You would want the same at the end of the BreakUp Sub, as you should have built up your string in the variable "Result" at that point.

    Likewise, you don't want the following in your BreakUp sub, you want to append the broken out digits in place of "numbers".
    Code:
                If numbers = starting Then
                    result = result & numbers
                End If
    I think I may have spoiled all the fun challenges at this point, other than general programing improvements. Let us know if you got it working.
    Last edited by passel; Dec 14th, 2017 at 03:06 PM.

  8. #8
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    363

    Re: Break up numbers and loop.

    Passel pointed it out in his post, but just to summarize the reason you are only getting "1 0 0" returned from the BreakUp function is because the Return statement is inside your For Loop. Once a Function executes a Return statement, that value is returned to the calling statement and the Function is exited.

    So BreakUp() is called, it parses out the number 100 into it's digits, "1 0 0" is returned and stored in lblBreakUp.Text, and that's the end of execution of the BreakUp Function, even though the loop condition was never met.

    So the Return statement has to come after the For statement, and inside of the For statement you just have to ensure that you are properly appending each value into one multi-line string, like you are already doing in the Build Function using string appending and vbCrLf characters.

  9. #9

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    3

    Re: Break up numbers and loop.

    I would like to thank all of you guys for helping me out. You have tought and helped me more then my teacher ever has. Thanks to you all it is now working as intended. It took me some time to figure out the hints but I finally got it.

    This is what the result for breakup() turned out in the end.
    Code:
        Function BreakUp()
            Dim starting As Integer = 100
            Dim ending As Integer = 125
            Dim result As String
    
    
            For numbers = starting To ending Step 1
                Call ThreeDigits(numbers, hundredsDigit, tensDigit, onesDigit)
                If numbers > starting Then
                    result &= String.Format("{0}{2}{1}{3}{1}{4}", vbNewLine, Space(5), hundredsDigit, tensDigit, onesDigit)
    
    
                End If
    
                If numbers = starting Then
                    result = hundredsDigit & Space(5) & tensDigit & Space(5) & onesDigit
                End If
    
    
            Next
            Return result
        End Function
    Thanks,
    Jacob

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,497

    Re: Break up numbers and loop.

    Personally, I don't like having an If condition in a loop that will only branch into the code once as that is wasting a relatively large amount of time because it still has to evaluate that if condition and branch around the code every other cycle (not as bad as it used to be before parallel processing pipelines in the CPU, but still undesirable), and also eliminate an if condition in a loop if it can be.
    So, I would pull the initial case out of the loop, removing the need for both If conditions within the loop.
    Code:
       Function BreakUp()
            Dim starting As Integer = 100
            Dim ending As Integer = 125
            Dim result As String
    
            ThreeDigits(starting, hundredsDigit, tensDigit, onesDigit)
            result = hundredsDigit & Space(5) & tensDigit & Space(5) & onesDigit
            starting += 1
    
            For numbers = starting To ending
                ThreeDigits(numbers, hundredsDigit, tensDigit, onesDigit)
                result &= String.Format("{0}{2}{1}{3}{1}{4}", vbNewLine, Space(5), hundredsDigit, tensDigit, onesDigit)
            Next
    
            Return result
        End Function
    Also, since For loop default to stepping by positive 1, you rarely see the Step option specified for default case.
    p.s. Also I don't use "Call" in my method calls.

    p.p.s. And just for additional information, if there was a need for a two case If condition in a loop, it would have been better to use an If .... Then .... Else .... Endif form as you would only be doing one If test, rather than two.
    Code:
        Function BreakUp()
            Dim starting As Integer = 100
            Dim ending As Integer = 125
            Dim result As String
    
    
            For numbers = starting To ending Step 1
                Call ThreeDigits(numbers, hundredsDigit, tensDigit, onesDigit)
    
                If numbers = starting Then
                    result = hundredsDigit & Space(5) & tensDigit & Space(5) & onesDigit
    
                Else
                    result &= String.Format("{0}{2}{1}{3}{1}{4}", vbNewLine, Space(5), hundredsDigit, tensDigit, onesDigit)
    
                End If
            Next
    
            Return result
        End Function
    Last edited by passel; Dec 15th, 2017 at 04:36 AM.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,188

    Re: Break up numbers and loop.

    Also, neither the Call nor the Step 1 does anything useful. The step is 1 by default, so if you don't specify a step, that's what you get. Therefore, the only real use for Step is if you want to step by anything OTHER than 1. Doesn't do any harm, though.
    My usual boring signature: Nothing

  12. #12
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Break up numbers and loop.

    Am I missing something or is there a reason we didn't get the digits by converting the number to a String and accessing its Char elements? That's a popular way to do it, too, and if VB didn't have some weird aggressive parameter binding we could:
    Code:
    Dim someNumber = 123
    
    Dim digits = someNumber.ToString().ToCharArray()
    Dim broken = String.Join(digits, " ")
    The only reason this doesn't work as-is is something I tried to file as a bug but the VB team said is by design. Even with option Strict on, the VB compiler decides to convert digits from "array of Char" to String instead of binding to the String.Join() overload that accepts "array of Char". There's other solutions but I'm just mad this one doesn't work due to VB quirkiness.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  13. #13
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,497

    Re: Break up numbers and loop.

    Quote Originally Posted by Sitten Spynne View Post
    Am I missing something or is there a reason we didn't get the digits by converting the number to a String and accessing its Char elements? ...
    I'm not sure if you're missing something or not. The OP said he was a student, and provided the code he was using to do the job that wasn't working. His assumption was that there was something simple wrong with his existing code. We were just attempting to help him fix his existing code, by using debugging methods to see where the code goes wrong, rather than provide a different approach to how the output could be generated.

    Now that his existing code has been fixed, then perhaps suggesting alternate approaches is an appropriate consideration.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width