dcsimg
Results 1 to 14 of 14

Thread: Visual Basic Do while loop condition for termination.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2019
    Posts
    67

    Visual Basic Do while loop condition for termination.

    I am converting my program that has a whole lot of String Concercation to one using StringBuilder.

    I am not too sure about the logic involved with telling the routine when to stop when all the data has been processed.

    Code:
    Public Sub ToFile(ByVal StrPadding As Integer())
        'Declare new StringBuilder Dim.
        Dim sb As Stringbuilder = New StringBuilder()
        '   Process data until no more calls to ToFile.
        Do While CStr(StrPadding() <> ""
    
    '   StringBuilder stuff will go here
    
       End Do
    Is the logic correct in that the processing will stop when no more calls to "Tofile" stops being made?
    Whoops...I forgot that the data in StrPadding may be numerical...in that case, "Do While CStr(StrPadding() <> " should likely be "Do While CStr(StrPadding() = 0", not an empty string.
    Last edited by Bushranger; Jan 14th, 2020 at 11:26 AM.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,796

    Re: Visual Basic Do while loop condition for termination.

    StrPadding is an integer array? Then CStr(StrPadding()) will never work .... you can't shove a whole array into a function that's not expecting an array.
    If the intent is to loop through the values of the array... then loop through the items in the array...

    Code:
    Public Sub ToFile(ByVal PaddingValues As Integer())
        'Declare new StringBuilder Dim.
        Dim sb As Stringbuilder = New StringBuilder()
        '   Process data until no more calls to ToFile. <---- you're in toFile... so just what does this mean?
        For Each PV as Integer in PaddingValues
        
    '   StringBuilder stuff will go here
    
       Next
    Renamed some stuff there...
    But there you go... a nice simple for each loop that loops through each element in the array.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2019
    Posts
    67

    Re: Visual Basic Do while loop condition for termination.

    you're in toFile... so just what does this mean?
    It was a comment. ToFile is a procedure. What I meant by the comment is that it should quit when there are no more data being passed to that procedure.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2019
    Posts
    67

    Re: Visual Basic Do while loop condition for termination.

    Quote Originally Posted by techgnome View Post
    StrPadding is an integer array? Then CStr(StrPadding()) will never work .... you can't shove a whole array into a function that's not expecting an array.
    If the intent is to loop through the values of the array... then loop through the items in the array...

    Code:
    Public Sub ToFile(ByVal PaddingValues As Integer())
        'Declare new StringBuilder Dim.
        Dim sb As Stringbuilder = New StringBuilder()
        '   Process data until no more calls to ToFile. <---- you're in toFile... so just what does this mean?
        For Each PV as Integer in PaddingValues
        
    '   StringBuilder stuff will go here
    
       Next
    Renamed some stuff there...
    But there you go... a nice simple for each loop that loops through each element in the array.

    -tg
    What I am trying to accomplish is to change this call to a procedure that writes 10 digits to a text box (it works, but looking to improve performance by changing from dealing with strings:
    Code:
    Public Sub ToFile(ByVal StrPadding As Integer())
            Dim i As Integer
            'Writes the encrypted text to Form3, textbox2.
            For i = 0 To 9
                Form3.TextBox2.Text &= CStr(StrPadding(i))
            Next
        End Sub
    To an equivalent procedure, but using StringBuilder when calls send the data (a string of 10 digits) with calls typical of these:
    Code:
      Case "80"
                        StrPadding = AddPad()
                        StrPadding(7) = GetEven()
                        Call ToFile(StrPadding)
                        StrPadding = AddPad()
                        StrPadding(7) = GetEven()
                        Call ToFile(StrPadding) 'Each Call ToFile sends 10 random digits to the ToFile procedure.
                        StrPadding = AddPad()
                        StrPadding(7) = GetEven()
                        Call ToFile(StrPadding)
                        StrPadding = AddPad()
                        StrPadding(1) = 0
                        Call ToFile(StrPadding)
                        StrPadding = AddPad()
                        StrPadding(7) = GetEven()
                        Call ToFile(StrPadding)
                        StrPadding = AddPad()
                        StrPadding(7) = GetEven()
                        Call ToFile(StrPadding)
                        StrPadding = AddPad()
                        StrPadding(7) = GetEven()
                        Call ToFile(StrPadding)
                        StrPadding = AddPad()
                        StrPadding(7) = GetEven()
                        Call ToFile(StrPadding)
                        StrPadding = AddPad()
                        StrPadding(7) = GetEven()
                        Call ToFile(StrPadding)
                        StrPadding = AddPad()
                        StrPadding(9) = 0
                        Call ToFile(StrPadding)
    Last edited by Bushranger; Jan 14th, 2020 at 04:58 PM.

  5. #5
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    1,006

    Re: Visual Basic Do while loop condition for termination.

    Holy cow. You are doing exactly what I was afraid you are doing in your code.

    This is how the flow of your program should work.

    1. User inputs string to encrypt.
    2. Code takes that string and runs through the encryption process and produces encrypted result.
    3. Code then takes that encrypted result and does whatever you want it to do (display it, write it to a file, etc.)

    At no point in step 2 should anything be displayed in a textbox, written to a file, etc. That should only be done when the encryption process is complete. Based the small amount of code you've included, I'm assuming you are calling "ToFile" repeatedly during the "encryption" process, which means that Form3.TextBox2.Text is being updated...hundreds? Thousands? Millions of times? Can't tell because we don't have more of your code, but writing anything to Form3.TextBox2.Text more than once is too many times.

    Good luck.

    Edit: Also, please start getting in to the habit of giving forms and controls meaningful names. Keeping the default names can make code really hard to understand for people who didn't write the code.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2019
    Posts
    67

    Re: Visual Basic Do while loop condition for termination.

    [QUOTE]Holy cow. You are doing exactly what I was afraid you are doing in your code.
    1. User inputs string to encrypt.
    Yes, that is what happens.

    2. Code takes that string and runs through the encryption process and produces encrypted result.
    Yes...does that.

    3. Code then takes that encrypted result and does whatever you want it to do (display it, write it to a file, etc.
    I used to just write it to a disk file after encrypting, but it took so long with no feedback to the user, my reasoning was that they may think that the program had locked-up and was not doing anything. So the textbox being written to as the program ran was meant for reassurance that something was indeed happening and they would not try to exit the program before it was finished.
    At no point in step 2 should anything be displayed in a textbox, written to a file, etc. That should only be done when the encryption process is complete.
    That is my intention when I convert to StringBuilder...only write to disk when processing is complete.

    Based the small amount of code you've included, I'm assuming you are calling "ToFile" repeatedly during the "encryption" process, which means that Form3.TextBox2.Text is being updated...hundreds? Thousands? Millions of times? Can't tell because we don't have more of your code, but writing anything to Form3.TextBox2.Text more than once is too many times.
    If and when I get things processing faster (as fast as it decrypts), I can dispense with the writing the encrypted code to the textbox...it is just an unintelligible string of digits anyway.

    Edit: Also, please start getting in to the habit of giving forms and controls meaningful names. Keeping the default names can make code really hard to understand for people who didn't write the code.
    Yes, I could have done better with meaningful names, however, that is the way I did it, way too much trouble to change them now...and this is very likely my "last rodeo"...I cannot envision a reason to ever write a program again. Nevertheless, if there is some unlikely event that changes that, I will endeavor to use meaningful names as suggested.

  7. #7
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: Visual Basic Do while loop condition for termination.

    Quote Originally Posted by Bushranger View Post
    I used to just write it to a disk file after encrypting, but it took so long with no feedback to the user, my reasoning was that they may think that the program had locked-up and was not doing anything. So the textbox being written to as the program ran was meant for reassurance that something was indeed happening and they would not try to exit the program before it was finished.
    As OptionBase1 says, you should never update TextBoxes or other Form Controls in a loop. For one thing, the GUI is only updated when it is idle; i.e. after the loop exits and your code isn't running (although there are a couple of exceptions to that). For another thing, it can really affect performance as you have seen.

    If your code is taking a long time to execute, then you should indeed give some sort of feedback to the user. There are various ways of doing that, varying in complexity. The simplest way is to use a progress bar, which is one of the exceptions I mentioned above; the ProgressBar Control is specifically designed to update its GUI while your code is running in a loop.


    Quote Originally Posted by Bushranger View Post
    That is my intention when I convert to StringBuilder...only write to disk when processing is complete.

    If and when I get things processing faster (as fast as it decrypts), I can dispense with the writing the encrypted code to the textbox...it is just an unintelligible string of digits anyway.
    It's hard to give specific advice on the best way to integrate a StringBuilder into your code without a better understanding of what your code is doing. However, based on the little bits you have shown so far, I'd probably do something like the following:

    First, I'd rewrite the ToFile Sub as a Function that converts the StrPadding integer array to a string and returns that string:
    Code:
    Private Function ToFile(ByVal StrPadding As Integer()) As String
        Dim padding = String.Concat(StrPadding)
        Return padding
    End Function
    The single call to String.Concat is much more efficient than repeatedly calling someString &= someOtherString in a loop, particulary when there are lots of iterations.

    Then I'd use a StringBuilder in your encryption code, appending the output from the ToFile function to the string builder. Once all the input 'character digits' have been processed and all the padding added to the StringBuilder, that's when you convert the StringBuilder to an actual String and write it to the TextBox or File or where ever.
    Code:
    Dim AssignedNumArray() As String
    ' code to  populate  AssignedNumArray
    
    
    
    Dim paddingBuilder As New StringBuilder
    
    
    For Each InToCode In AssignedNumArray
    
        Select Case InToCode
            'Lots of Case code blocks here.....
    
            Case "79"
                ' code for "79"
    
            Case "80"
                StrPadding = AddPad()
                StrPadding(7) = GetEven()
                paddingBuilder.Append(ToFile(StrPadding))
    
                StrPadding = AddPad()
                StrPadding(7) = GetEven()
                paddingBuilder.Append(ToFile(StrPadding)) 'Each Call ToFile0 sends 10 random digits to the ToFile0 procedure.
    
                StrPadding = AddPad()
                StrPadding(7) = GetEven()
                paddingBuilder.Append(ToFile(StrPadding))
    
                StrPadding = AddPad()
                StrPadding(1) = 0
                paddingBuilder.Append(ToFile(StrPadding))
    
                StrPadding = AddPad()
                StrPadding(7) = GetEven()
                paddingBuilder.Append(ToFile(StrPadding))
    
                StrPadding = AddPad()
                StrPadding(7) = GetEven()
                paddingBuilder.Append(ToFile(StrPadding))
    
                StrPadding = AddPad()
                StrPadding(7) = GetEven()
                paddingBuilder.Append(ToFile(StrPadding))
    
                StrPadding = AddPad()
                StrPadding(7) = GetEven()
                paddingBuilder.Append(ToFile(StrPadding))
    
                StrPadding = AddPad()
                StrPadding(7) = GetEven()
                paddingBuilder.Append(ToFile(StrPadding))
    
                StrPadding = AddPad()
                StrPadding(9) = 0
                paddingBuilder.Append(ToFile(StrPadding))
    
            Case "81"
                ' code for "81"
    
            Case "82"
    
                'etc
    
                'etc
    
        End Select
    Next
    
    Form3.TextBox2.Text = paddingBuilder.ToString
    I mocked up a very basic test:
    Your original code (appending each padding char to a TextBox) took about 3.5 minutes to encrypt 120 characters.
    Using the StringBuilder as above took about 35 milliseconds to do the same.


    Note that as the ToFile function does so little, I'd probably do away with it and just inline the conversion from integer array to string:
    Code:
    StrPadding = AddPad()
    StrPadding(7) = GetEven()
    paddingBuilder.Append(String.Concat(StrPadding))
    ...and then go on to try to reduce the complexity of all the repeated code in the Select Case blocks :-)
    Last edited by Inferrd; Jan 14th, 2020 at 09:09 PM.

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,796

    Re: Visual Basic Do while loop condition for termination.

    Note that as the ToFile function does so little, I'd probably do away with it and just inline the conversion from integer array to string:
    Careful... we don't really know what ToFile does... I'm pretty sure based on comments though it does NOT write to a file, so it's name is dubious, but that's another matter and not something I'm going to get into at the moment...
    All we know about ToFile is that "StringBuilder stuff will go here" ... but what that means... shrug...
    Now, if things are that simple... then sure... but since the data that's passed in are 10 random integers and not the data itself...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2019
    Posts
    67

    Re: Visual Basic Do while loop condition for termination.

    Quote Originally Posted by techgnome View Post
    Careful... we don't really know what ToFile does... I'm pretty sure based on comments though it does NOT write to a file, so it's name is dubious, but that's another matter and not something I'm going to get into at the moment...
    All we know about ToFile is that "StringBuilder stuff will go here" ... but what that means... shrug...
    Now, if things are that simple... then sure... but since the data that's passed in are 10 random integers and not the data itself...

    -tg
    ToFile originally wrote the data to a disk file. However, I changed it to write it to a textbox instead because I though that it was better (more efficient that writing shot stings to the disk) And when the processing was finished, I had a button on the form that wrote it to the disk file. I left the orignial name of the procedure "ToFile" despite the fact it no longer did that.

  10. #10
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: Visual Basic Do while loop condition for termination.

    Quote Originally Posted by techgnome View Post
    Careful... we don't really know what ToFile does... I'm pretty sure based on comments though it does NOT write to a file, so it's name is dubious, but that's another matter and not something I'm going to get into at the moment...
    All we know about ToFile is that "StringBuilder stuff will go here" ... but what that means... shrug...
    Now, if things are that simple... then sure... but since the data that's passed in are 10 random integers and not the data itself...

    -tg
    Agreed, but I'm assuming that all it's doing is joining 10 integer values into a single string, so that it can be added to the end of the encrypted output string, and I'm basing that assumption on the original definition of the ToFile method that Bushranger gave in Post#4. ...But you never know :-)

    It also occurred to me that Bushranger will have 94 Case statements, each with at least 10 calls to ToFile. So if he ever is likely to change the functionality of ToFile, then it would indeed be better to leave it as a method. It's a damn sight easier to change one method definition rather than the 940 lines of code that called it.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Dec 2019
    Posts
    67

    Re: Visual Basic Do while loop condition for termination.

    Note that as the ToFile function does so little, I'd probably do away with it and just inline the conversion from integer array to string:
    I have been thinking about doing that...it occured to me that it might be the way to go to simplify things.

  12. #12
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,450

    Re: Visual Basic Do while loop condition for termination.

    I asked in your other thread if each encrypted character is a separate string or if you concatenate them but never got an answer. Because if you do concatenate them then thats going to be a major time hit so I'll ask one more time.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Dec 2019
    Posts
    67

    Re: Visual Basic Do while loop condition for termination.

    Quote Originally Posted by wes4dbt View Post
    I asked in your other thread if each encrypted character is a separate string or if you concatenate them but never got an answer. Because if you do concatenate them then thats going to be a major time hit so I'll ask one more time.
    Yes each text character in a text message is converted into units of 100 numeric characters by units of 10 and then concatenated together in one long string.

  14. #14
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,450

    Re: Visual Basic Do while loop condition for termination.

    Quote Originally Posted by Bushranger View Post
    Yes each text character in a text message is converted into units of 100 numeric characters by units of 10 and then concatenated together in one long string.
    Yeah, my guess is that step is costing you a lot of time. Using a StringBuilder is a good idea but I don't think it will solve your problem. It should be easy to test if concatenating into one long string is causing you a major delay. Just comment out that section and run the program. I know it wont decrypt but at least you'll know if that's the issue.

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