Results 1 to 5 of 5

Thread: [RESOLVED] [VB6] Do these two codes give same effect?

  1. #1

    Thread Starter
    New Member Sgt. Hairy's Avatar
    Join Date
    Dec 2005
    Posts
    12

    Resolved [RESOLVED] [VB6] Do these two codes give same effect?

    Hi.
    I had this code that was long and probably made the program run slower. And also took up lots of disk space.

    I recently came up with another code that makes me wonder if it gives the same effect. What do you think?

    New code:

    VB Code:
    1. Public Function ReLoadGrid(MyFrm As Form, MyFlex As MSHFlexGrid, MyTab As SSTab)
    2. Dim Cntr As Integer, Tmp As Integer
    3.     For Cntr = 65 To 90
    4.         If Left$(MyFrm.txtFName.Text, 1) = Chr(Cntr) Then
    5.             MyTab.Tab = Tmp
    6.             LoadGrid MyTab.Caption, MyFlex
    7.             Tmp = Tmp + 1
    8.         End If
    9.     Next Cntr
    10. End Function

    Old code:
    VB Code:
    1. Public Function ReLoadGrid2(MyFrm As Form, MyFlex As MSHFlexGrid, MyTab As SSTab)
    2.     With MyFrm
    3.         Select Case Left$(.txtFName.Text, 1)
    4.             Case Chr(65)
    5.                 MyTab.Tab = 0
    6.                 LoadGrid MyTab.Caption, MyFlex
    7.             Case Chr(66)
    8.                 MyTab.Tab = 1
    9.                 LoadGrid MyTab.Caption, MyFlex
    10.             Case Chr(67)
    11.                 MyTab.Tab = 2
    12.                 LoadGrid MyTab.Caption, MyFlex
    13.             Case Chr(68)
    14.                 MyTab.Tab = 3
    15.                 LoadGrid MyTab.Caption, MyFlex
    16.             Case Chr(69)
    17.                 MyTab.Tab = 4
    18.                 LoadGrid MyTab.Caption, MyFlex
    19.             Case Chr(70)
    20.                 MyTab.Tab = 5
    21.                 LoadGrid MyTab.Caption, MyFlex
    22.             Case Chr(71)
    23.                 MyTab.Tab = 6
    24.                 LoadGrid MyTab.Caption, MyFlex
    25.             Case Chr(72)
    26.                 MyTab.Tab = 7
    27.                 LoadGrid MyTab.Caption, MyFlex
    28.             Case Chr(73)
    29.                 MyTab.Tab = 8
    30.                 LoadGrid MyTab.Caption, MyFlex
    31.             Case Chr(74)
    32.                 MyTab.Tab = 9
    33.                 LoadGrid MyTab.Caption, MyFlex
    34.             Case Chr(75)
    35.                 MyTab.Tab = 10
    36.                 LoadGrid MyTab.Caption, MyFlex
    37.             Case Chr(76)
    38.                 MyTab.Tab = 11
    39.                 LoadGrid MyTab.Caption, MyFlex
    40.             Case Chr(77)
    41.                 MyTab.Tab = 12
    42.                 LoadGrid MyTab.Caption, MyFlex
    43.             Case Chr(78)
    44.                 MyTab.Tab = 13
    45.                 LoadGrid MyTab.Caption, MyFlex
    46.             Case Chr(79)
    47.                 MyTab.Tab = 14
    48.                 LoadGrid MyTab.Caption, MyFlex
    49.             Case Chr(80)
    50.                 MyTab.Tab = 15
    51.                 LoadGrid MyTab.Caption, MyFlex
    52.             Case Chr(81)
    53.                 MyTab.Tab = 16
    54.                 LoadGrid MyTab.Caption, MyFlex
    55.             Case Chr(82)
    56.                 MyTab.Tab = 17
    57.                 LoadGrid MyTab.Caption, MyFlex
    58.             Case Chr(83)
    59.                 MyTab.Tab = 18
    60.                 LoadGrid MyTab.Caption, MyFlex
    61.             Case Chr(84)
    62.                 MyTab.Tab = 19
    63.                 LoadGrid MyTab.Caption, MyFlex
    64.             Case Chr(85)
    65.                 MyTab.Tab = 20
    66.                 LoadGrid MyTab.Caption, MyFlex
    67.             Case Chr(86)
    68.                 MyTab.Tab = 21
    69.                 LoadGrid MyTab.Caption, MyFlex
    70.             Case Chr(87)
    71.                 MyTab.Tab = 22
    72.                 LoadGrid MyTab.Caption, MyFlex
    73.             Case Chr(88)
    74.                 MyTab.Tab = 23
    75.                 LoadGrid MyTab.Caption, MyFlex
    76.             Case Chr(89)
    77.                 MyTab.Tab = 24
    78.                 LoadGrid MyTab.Caption, MyFlex
    79.             Case Chr(90)
    80.                 MyTab.Tab = 25
    81.                 LoadGrid MyTab.Caption, MyFlex
    82.         End Select
    83.     End With
    84. End Function
    Guess who's back?

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: [VB6] Do these two codes give same effect?

    Old code would be faster, but new code would work if you have more than one thing to add, whereas old code would have to be called for each item.

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

    Re: [VB6] Do these two codes give same effect?

    In terms of speed, the old version will be marginally faster - but unless this is being run over and over again in a short space of time you will not notice.


    The two versions aren't quite the same, as the value of MyTab.Tab is set to different values. To make the new code functionally the same as the old code, remove the Tmp variable and set the value of MyTab.Tab like this:
    VB Code:
    1. MyTab.Tab = Cntr - 65
    You could also add "Exit For" after the "LoadGrid" line (so that you do not loop more than you need to), and store the value of your Left$ calculation (so that you don't do the same work repeatedly), and store the Ascii code of this rather than using Chr on the loop variable (less work).

    Alternatively, you could get rid of the loop altogether, and use this:
    VB Code:
    1. Public Function ReLoadGrid(MyFrm As Form, MyFlex As MSHFlexGrid, MyTab As SSTab)
    2.  
    3. Dim CharacterCode as Integer
    4.   CharacterCode = Asc(Left$(MyFrm.txtFName.Text, 1))
    5.  
    6.   If (CharacterCode >= 65) And (CharacterCode <= 90) Then
    7.      MyTab.Tab = CharacterCode - 65
    8.      LoadGrid MyTab.Caption, MyFlex
    9.   End if
    10.  
    11. End Function
    This should be a bit faster than either version you posted.


    There are two things I'm confused about:
    1) Why is this a Function, rather than a Sub (you do not return a value)?
    2) why do you pass the form as a parameter and use MyFrm.txtFName instead of passing the textbox as a parameter like the other controls?

  4. #4

    Thread Starter
    New Member Sgt. Hairy's Avatar
    Join Date
    Dec 2005
    Posts
    12

    Re: [VB6] Do these two codes give same effect?

    I guess I'll stay with the old code if it seems better to you guys.
    Thanks for all the help.

    1) Why is this a Function, rather than a Sub (you do not return a value)?
    I was a newbie when I composed that. I will change it to sub. Thanks for pointing it out.

    2) why do you pass the form as a parameter and use MyFrm.txtFName instead of passing the textbox as a parameter like the other controls?
    I had a few different forms with the same control name.
    Guess who's back?

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

    Re: [VB6] Do these two codes give same effect?

    Quote Originally Posted by Sgt. Hairy
    I guess I'll stay with the old code if it seems better to you guys.
    Actually I think my version is better, in terms of speed and (for me) readability.

    I had a few different forms with the same control name.
    I would change it myself, so that if you change the control name on one of the forms that use it, you will get an error in the form - rather than in this code. (it is better design that way, as the code is more 'abstracted' from the forms).

    Thanks for all the help.
    No problem, it's what we are here for

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