|
-
Jan 14th, 2006, 12:25 AM
#1
Thread Starter
New Member
[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:
Public Function ReLoadGrid(MyFrm As Form, MyFlex As MSHFlexGrid, MyTab As SSTab)
Dim Cntr As Integer, Tmp As Integer
For Cntr = 65 To 90
If Left$(MyFrm.txtFName.Text, 1) = Chr(Cntr) Then
MyTab.Tab = Tmp
LoadGrid MyTab.Caption, MyFlex
Tmp = Tmp + 1
End If
Next Cntr
End Function
Old code:
VB Code:
Public Function ReLoadGrid2(MyFrm As Form, MyFlex As MSHFlexGrid, MyTab As SSTab)
With MyFrm
Select Case Left$(.txtFName.Text, 1)
Case Chr(65)
MyTab.Tab = 0
LoadGrid MyTab.Caption, MyFlex
Case Chr(66)
MyTab.Tab = 1
LoadGrid MyTab.Caption, MyFlex
Case Chr(67)
MyTab.Tab = 2
LoadGrid MyTab.Caption, MyFlex
Case Chr(68)
MyTab.Tab = 3
LoadGrid MyTab.Caption, MyFlex
Case Chr(69)
MyTab.Tab = 4
LoadGrid MyTab.Caption, MyFlex
Case Chr(70)
MyTab.Tab = 5
LoadGrid MyTab.Caption, MyFlex
Case Chr(71)
MyTab.Tab = 6
LoadGrid MyTab.Caption, MyFlex
Case Chr(72)
MyTab.Tab = 7
LoadGrid MyTab.Caption, MyFlex
Case Chr(73)
MyTab.Tab = 8
LoadGrid MyTab.Caption, MyFlex
Case Chr(74)
MyTab.Tab = 9
LoadGrid MyTab.Caption, MyFlex
Case Chr(75)
MyTab.Tab = 10
LoadGrid MyTab.Caption, MyFlex
Case Chr(76)
MyTab.Tab = 11
LoadGrid MyTab.Caption, MyFlex
Case Chr(77)
MyTab.Tab = 12
LoadGrid MyTab.Caption, MyFlex
Case Chr(78)
MyTab.Tab = 13
LoadGrid MyTab.Caption, MyFlex
Case Chr(79)
MyTab.Tab = 14
LoadGrid MyTab.Caption, MyFlex
Case Chr(80)
MyTab.Tab = 15
LoadGrid MyTab.Caption, MyFlex
Case Chr(81)
MyTab.Tab = 16
LoadGrid MyTab.Caption, MyFlex
Case Chr(82)
MyTab.Tab = 17
LoadGrid MyTab.Caption, MyFlex
Case Chr(83)
MyTab.Tab = 18
LoadGrid MyTab.Caption, MyFlex
Case Chr(84)
MyTab.Tab = 19
LoadGrid MyTab.Caption, MyFlex
Case Chr(85)
MyTab.Tab = 20
LoadGrid MyTab.Caption, MyFlex
Case Chr(86)
MyTab.Tab = 21
LoadGrid MyTab.Caption, MyFlex
Case Chr(87)
MyTab.Tab = 22
LoadGrid MyTab.Caption, MyFlex
Case Chr(88)
MyTab.Tab = 23
LoadGrid MyTab.Caption, MyFlex
Case Chr(89)
MyTab.Tab = 24
LoadGrid MyTab.Caption, MyFlex
Case Chr(90)
MyTab.Tab = 25
LoadGrid MyTab.Caption, MyFlex
End Select
End With
End Function
-
Jan 14th, 2006, 12:56 AM
#2
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.
-
Jan 14th, 2006, 02:18 AM
#3
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:
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:
Public Function ReLoadGrid(MyFrm As Form, MyFlex As MSHFlexGrid, MyTab As SSTab)
Dim CharacterCode as Integer
CharacterCode = Asc(Left$(MyFrm.txtFName.Text, 1))
If (CharacterCode >= 65) And (CharacterCode <= 90) Then
MyTab.Tab = CharacterCode - 65
LoadGrid MyTab.Caption, MyFlex
End if
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?
-
Jan 16th, 2006, 12:29 AM
#4
Thread Starter
New Member
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.
-
Jan 16th, 2006, 07:02 AM
#5
Re: [VB6] Do these two codes give same effect?
 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).
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|