|
-
May 28th, 2002, 04:30 PM
#1
Declairing Vars - What's better?
Sups,
This question is buggin me and Id really like to the answer.
Lets say I have many functions and subs in my project, and i'm using For-Next loops in those functions/subs.
My question is, what is the best way to declaire the i var:
1. Decliare it once in General Declairations as public
2. Decliare it one by one as private in each function/sub
?
Tnx.
-
May 28th, 2002, 04:32 PM
#2
-= B u g S l a y e r =-
-
May 28th, 2002, 04:33 PM
#3
PowerPoster
I declare it in each sub or function. The way my code is written, it wouldn't matter, but that's just the way I like to do it.
Some people will say that it's possible for it to retain a value and screw things up, but in a For... Next loop, the start and end values are explicitly declared, so that really can't happen.
-
May 28th, 2002, 04:38 PM
#4
1 thing that is good is that if for example i write a function, and then i need to use it in a new project, i can just copy and paste it, without adding and changin the code.
But I mean as for the memory thing, what is better and what is takin less resources?
-
May 28th, 2002, 04:39 PM
#5
-= B u g S l a y e r =-
u will go flat on your nose sooner or later .... just take a look at this EXCELENT sample 
in form
VB Code:
Private Sub Command1_Click()
test
End Sub
in a module
VB Code:
Option Explicit
Private i As Integer
Public Sub test()
For i = 0 To 32
MsgBox GetSomeData
Next i
End Sub
Private Function GetSomeData() As String
Dim s As String
For i = 0 To 3
s = s & i
Next i
GetSomeData = s
End Function
damn I'm creative
must be the worst sample ever, but I think(uhh hoping ) my point gets throuhg
-
May 28th, 2002, 04:41 PM
#6
PowerPoster
the statement "in a For... Next loop, the start and end values are explicitly declared, so that really can't happen" is patently untrue.
If the for ... next section contains a function call, which it certainly could, and that function uses the same public loop index then you absolutely WILL screw yourself up.
Thus, NEVER use public loop counter. Peet has it right. If you write enough code over a long period of time and you use public counters, you WILL screw up eventually.
-
May 28th, 2002, 04:46 PM
#7
Creative like nuts! Good example man.
So ya sayin I should declaire it in each and every function/sub.
But what about the memory? What is better when it comes to memory if ne?
-
May 28th, 2002, 04:46 PM
#8
PowerPoster
Originally posted by phinds
the statement "in a For... Next loop, the start and end values are explicitly declared, so that really can't happen" is patently untrue.
If the for ... next section contains a function call, which it certainly could, and that function uses the same public loop index then you absolutely WILL screw yourself up.
Thus, NEVER use public loop counter. Peet has it right. If you write enough code over a long period of time and you use public counters, you WILL screw up eventually.
Sorry for the bad info. I didn't think about it the way Peet used in his example which is why I don't declare public counters - something bad is bound to happen that you didn't think about. I think the memory savings are nil because the variable goes out of scope when you leave the sub anyway.
Personally, I think it's a waste of time to try to squeeze every last ounce of efficiency with things like this. Most people write the same string all over their apps instead of declaring one constant. That's the sort of thing that should be looked at because strings consume a lot of memory compared to numeric data types.
-
May 28th, 2002, 04:48 PM
#9
-= B u g S l a y e r =-
-
May 28th, 2002, 04:48 PM
#10
Originally posted by phinds
the statement "in a For... Next loop, the start and end values are explicitly declared, so that really can't happen" is patently untrue.
If the for ... next section contains a function call, which it certainly could, and that function uses the same public loop index then you absolutely WILL screw yourself up.
Thus, NEVER use public loop counter. Peet has it right. If you write enough code over a long period of time and you use public counters, you WILL screw up eventually.
Yea I was thinkin the way you and peet...but what is better when it comes to memory? is there a difference?
-
May 28th, 2002, 04:53 PM
#11
I suggest that you never use "i" or any other one-character variable names. How much more trouble is it to use something like intIndex? A name like that is self-documenting in two ways in that it describes both its type and use. BTW, "i", "j" and "k" are left over from the old days of Fortran.
-
May 28th, 2002, 04:54 PM
#12
-= B u g S l a y e r =-
-
May 28th, 2002, 04:56 PM
#13
PowerPoster
Yes... that's exactly what I said. It was the only part I got right, as a matter of fact.
-
May 28th, 2002, 04:57 PM
#14
Originally posted by MartinLiss
I suggest that you never use "i" or any other one-character variable names. How much more trouble is it to use something like intIndex? A name like that is self-documenting in two ways in that it describes both its type and use. BTW, "i", "j" and "k" are left over from the old days of Fortran.
Im always givin actual names to my vars. But as for I, J, K, i'm using them only in for-next loops when i need to count number of loops.
-
May 28th, 2002, 04:58 PM
#15
-= B u g S l a y e r =-
-
May 28th, 2002, 04:59 PM
#16
Yes, you right.
-
May 28th, 2002, 04:59 PM
#17
-= B u g S l a y e r =-
-
May 28th, 2002, 05:00 PM
#18
-
May 28th, 2002, 05:02 PM
#19
-= B u g S l a y e r =-
Originally posted by cafeenman
... I think the memory savings are nil because the variable goes out of scope when you leave the sub anyway....
again..
sorry man .. didn't see that at first
-
May 28th, 2002, 05:03 PM
#20
PowerPoster
It's cool. I wasn't trying to say "I said it first." I was just saying that I wasn't totally screwed up in my response. Just mostly messed up.
-
May 28th, 2002, 05:05 PM
#21
-= B u g S l a y e r =-
-
May 28th, 2002, 05:19 PM
#22
Originally posted by Stiletto
Im always givin actual names to my vars. But as for I, J, K, i'm using them only in for-next loops when i need to count number of loops.
Here are two examples of the same loop (the first one modified) from an app of mine. I think the second one is better because when I look at the code 6 months from now I don't have to remember what "i" is and I don't have to scroll up the code window to find out. It's a small difference, but I believe it's much more programmer-friendly.
VB Code:
For i = 0 To frmMain.chkOtherAttr.Count - 1
If frmMain.chkOtherAttr(i).Value = vbChecked Then
' The TranslateXSDDataType routine converts the xml element
' types to a string with the Case values.
Select Case TranslateXSDDataType(LCase$(gcolOtherAttr(i + 1).Name), _
TRANS_OTHER_ATTR)
Case "TextType"
Call oxmlElement.setAttribute(frmMain.chkOtherAttr(i).Caption, _
frmMain.txtOtherAttr(i).Text)
If LCase$(frmMain.chkOtherAttr(i).Caption) = NAME_ITEM Then
'******* 1.00.001 #5 Start *******
frmMain.txtOtherAttr(i).Text = _
CorrectCaseName("attribute", _
frmMain.txtOtherAttr(i).Text)
'******* 1.00.001 #5 End *********
If frmMain.tvwXML.SelectedItem.Text <> _
frmMain.txtOtherAttr(i).Text Then
' The user has changed the name of the attribute-element
' so we need to change the text of the tree node
frmMain.tvwXML.SelectedItem.Text = _
frmMain.txtOtherAttr(i).Text
gTree.AttributeName = frmMain.txtOtherAttr(i).Text
End If
End If
Case "BooleanType"
If frmMain.optOtherYesNo(i).OptYesValue = True Then
Call oxmlElement.setAttribute(frmMain.chkOtherAttr(i) _
.Caption, "yes")
Else
Call oxmlElement.setAttribute(frmMain.chkOtherAttr(i) _
.Caption, "no")
End If
Case "ComboBoxType"
Call oxmlElement.setAttribute(frmMain.chkOtherAttr(i).Caption, _
frmMain.cboOtherAttr(i) _
.List(frmMain.cboOtherAttr(i).ListIndex))
Case "PosIntType"
Call oxmlElement.setAttribute(frmMain.chkOtherAttr(i).Caption, _
frmMain.txtOtherAttrPosint(i).Text)
Case "FloatType"
Call oxmlElement.setAttribute(frmMain.chkOtherAttr(i).Caption, _
frmMain.txtOtherAttrFloat(i).Text)
Case Else
MsgBox ResolveResString(resUnexpectedData, _
"|1", TranslateXSDDataType _
(LCase$(gcolOtherAttr(i + 1).Name), _
TRANS_OTHER_ATTR), _
"|2", frmMain.fraMisc(fraOtherAttr).Caption, _
"|3", frmMain.chkOtherAttr(i).Caption), _
vbExclamation, _
ResolveResString(resValidationErrorTitle, _
"|1", frmMain.fraMisc(fraOtherAttr).Caption)
End Select
Else
Call oxmlElement.removeAttribute(frmMain.chkOtherAttr(i).Caption)
End If
Next
VB Code:
For intIndex = 0 To frmMain.chkOtherAttr.Count - 1
If frmMain.chkOtherAttr(intIndex).Value = vbChecked Then
' The TranslateXSDDataType routine converts the xml element
' types to a string with the Case values.
Select Case TranslateXSDDataType(LCase$(gcolOtherAttr(intIndex + 1).Name), _
TRANS_OTHER_ATTR)
Case "TextType"
Call oxmlElement.setAttribute(frmMain.chkOtherAttr(intIndex).Caption, _
frmMain.txtOtherAttr(intIndex).Text)
If LCase$(frmMain.chkOtherAttr(intIndex).Caption) = NAME_ITEM Then
'******* 1.00.001 #5 Start *******
frmMain.txtOtherAttr(intIndex).Text = _
CorrectCaseName("attribute", _
frmMain.txtOtherAttr(intIndex).Text)
'******* 1.00.001 #5 End *********
If frmMain.tvwXML.SelectedItem.Text <> _
frmMain.txtOtherAttr(intIndex).Text Then
' The user has changed the name of the attribute-element
' so we need to change the text of the tree node
frmMain.tvwXML.SelectedItem.Text = _
frmMain.txtOtherAttr(intIndex).Text
gTree.AttributeName = frmMain.txtOtherAttr(intIndex).Text
End If
End If
Case "BooleanType"
If frmMain.optOtherYesNo(intIndex).OptYesValue = True Then
Call oxmlElement.setAttribute(frmMain.chkOtherAttr(intIndex) _
.Caption, "yes")
Else
Call oxmlElement.setAttribute(frmMain.chkOtherAttr(intIndex) _
.Caption, "no")
End If
Case "ComboBoxType"
Call oxmlElement.setAttribute(frmMain.chkOtherAttr(intIndex).Caption, _
frmMain.cboOtherAttr(intIndex) _
.List(frmMain.cboOtherAttr(intIndex).ListIndex))
Case "PosIntType"
Call oxmlElement.setAttribute(frmMain.chkOtherAttr(intIndex).Caption, _
frmMain.txtOtherAttrPosint(intIndex).Text)
Case "FloatType"
Call oxmlElement.setAttribute(frmMain.chkOtherAttr(intIndex).Caption, _
frmMain.txtOtherAttrFloat(intIndex).Text)
Case Else
MsgBox ResolveResString(resUnexpectedData, _
"|1", TranslateXSDDataType _
(LCase$(gcolOtherAttr(intIndex + 1).Name), _
TRANS_OTHER_ATTR), _
"|2", frmMain.fraMisc(fraOtherAttr).Caption, _
"|3", frmMain.chkOtherAttr(intIndex).Caption), _
vbExclamation, _
ResolveResString(resValidationErrorTitle, _
"|1", frmMain.fraMisc(fraOtherAttr).Caption)
End Select
Else
Call oxmlElement.removeAttribute(frmMain.chkOtherAttr(intIndex).Caption)
End If
Next
-
May 28th, 2002, 05:21 PM
#23
PowerPoster
I'm not even going to pretend like I understand that code.
-
May 28th, 2002, 05:25 PM
#24
Stuck in the 80s
Wow. That's a lot of code for a little point.
-
May 28th, 2002, 06:25 PM
#25
Addicted Member
well i usually leave the single letter variables for integer counting. i j and k --and i use x and y for positioning.
-
May 28th, 2002, 06:27 PM
#26
Addicted Member
Originally posted by cafeenman
I'm not even going to pretend like I understand that code.
how can you-- the code references a lot of subs and functions.. who knows what they do
-
May 29th, 2002, 08:19 AM
#27
Originally posted by cafeenman
I'm not even going to pretend like I understand that code.
Hehe
-
May 29th, 2002, 08:23 AM
#28
What I mean is that for counting number of loop, I use one letter counters like I,J,H. But when it comes to vars or to count something that i need to use it (say counting number of users logged on), i always use actual names.
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
|