|
In the original coders defense, if there (previously) was a condition around the ReDim / nFoo = / j = lines, the logic makes perfect sense.
|
|
|
|
|
Graham Bradshaw wrote: In the original coders defense, if there (previously) was a condition around the ReDim / nFoo = / j = lines, the logic makes perfect sense.
Actually in one of the three cases this was true, but then one could do it like this (which is what i chose)
j = 0
ReDim nFoo(lvwCardGrp.ListItems.Count - 1)
For i = 1 To lvwCardGrp.ListItems.Count
if some_condition then
nFoo(j) = Mid$(lvwCardGrp.ListItems(i).Key, 3)
j = j + 1
endif
Next
ReDim nFoo(j - 1)
In this case only one allocation happens. And in the end free up the unnecessary space, without the preserve statement which needs to copy the data to a new location.
Learn from the mistakes of others, you may not live long enough to make them all yourself.
|
|
|
|
|
Shouldn't ReDim nFoo(j - 1) be ReDim Preserve nFoo(j - 1) ? As the code is, you reinitialise nFoo.
|
|
|
|
|
Graham Bradshaw wrote: Shouldn't ReDim nFoo(j - 1) be ReDim Preserve nFoo(j - 1)? As the code is, you reinitialise nFoo.
Yes, you are correct.
Thanks for pointing it out, must have slipped through my fingers while typing it over from the real code.
Note to myself: Learn to use Copy-Past
Learn from the mistakes of others, you may not live long enough to make them all yourself.
|
|
|
|
|
I don't see how. Unless I read it wrong, j is unnecessary, the array should be redim ed before the loop, we already know how many items it needs to hold.
|
|
|
|
|
PIEBALDconsult wrote: we already know how many items it needs to hold.
Not if you only add items to the array in some condition, which was the point I was trying to make. See the other reply to my post for an example.
|
|
|
|
|
Ah, I see, I did read it wrong.
|
|
|
|
|
That's just too bad. Way too bad.
|
|
|
|
|
Seen in a VBA app in lieu of commenting out/deleting 138 lines of junk code.
Yes, the VBA editor is a steaming pile of excrement, but any decent text editor will let you insert the ' via a global insert/replace.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
dan neely wrote: any decent text editor
Like Notepad?
|
|
|
|
|
maybe, I'd've used a regex to match on the start of a line. I know notepad doesn't do that, can it match on the newline char itself?
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
dan neely wrote: can it match on the newline char itself
Not that I've found, I usually resort to opening the file in Word to do that.
|
|
|
|
|
I'd recommend Notepad++ a free replacement for Notepad, with all those regex goodies you've been missing
|
|
|
|
|
It even forgets CTRL+Z after two usage instances. I would say, WordPad should be safe and reliable though it nags us against saving in plain text formats.
Vasudevan Deepak Kumar
Personal Homepage Tech Gossips
All the world's a stage,
And all the men and women merely players.
They have their exits and their entrances;
And one man in his time plays many parts... --William Shakespeare
|
|
|
|
|
Maybe the programmer thought they would enable that section of code later?
Even then, it is better, IMO, to comment out the section of code until it is needed.
Bill W
|
|
|
|
|
actually it was originally needed, but subsequently "commented out" code.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
even better than if (2 == 2)
|
|
|
|
|
The first 2 letters in the language should tell you why that code exists.
|
|
|
|
|
Exactly, any real programmer would write "if (!true)"
|
|
|
|
|
dan neely wrote: the VBA editor is a steaming pile of excrement
Well put.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
code
code
code
GOTO leap_over
dodgy code that should have been commented
leap_over:
continue from here
yet another use for GOTO
I still remember having to write your own code in FORTRAN rather than be a cut and paste merchant being pampered by colour coded Intellisense - ahh proper programming - those were the days
|
|
|
|
|
GOTO rules, lets start the fight to bring this wonderful statement back into acceptable use...
Just racking up the postings
|
|
|
|
|
I have no problems at all using GOTO in small sections of code.
Beats all that complicated logic and program flow nonsense
I still remember having to write your own code in FORTRAN rather than be a cut and paste merchant being pampered by colour coded Intellisense - ahh proper programming - those were the days
|
|
|
|
|
There is a book called "Code Complete", that says that GoTo statements should always be avoided unless you're coding with Fortran
|
|
|
|
|
I have to agree that goto should be (and can be) avoided in all but the most extreme circumstances.
Bill W
|
|
|
|