Thursday, October 18, 2007

VBScript - Forgotten Realms

There are times as a programmer that you just have to throw up your hands and say WTF!?! Well, today I had one of those situations. I was working in batch scripting and VBScript. Well, my batch scripting has become quite more advanced then it once had been thanks to a previous project (iCom) and its over night processing routines. Anyway, since I've been involved in nothing but integration projects for the past year or so, my development requirements has been few and far between.

Today, I needed to write a script that would sort the contents of a file. Simple task, yet, not something I'm going to attempt with the batch language. It is just too cumbersome. Therefore, in comes some VBScript. It has been a long time since I've worked with VB. Well over a year and a half, as that is when I graduated. So, to accomplish this, I need an array, a sort routine, and a way to read the file into the array. No big deal. This should be a five minute job. Sure, if you've worked in a VB IDE in the recent past.

Since I have not touched VB in such a time, I completely forgot about the caveats around the REDIM statement. I was troubleshooting this script for well over a half an hour because the array would grow but the contents would disappear. I'm troubleshooting the array population (while it is populating); I was troubleshooting the sort routine; I troubleshot everything so I thought. Anyway, as it turns out, between my population routine and the sort, my information was limited to a single record in my 148 element array. I was confounded. The problem existed in the population routine alright. The question was where. Finally, my very first lesson (which I've evidently long since forgot, its been well over 8 years) with working with arrays. If you don't put the key word Preserve. I could not believe that this little word came back to haunt me.

In VB, if you resize an array, and forget the Preserve, it will blank out everything that was previously in the array. Other languages (PHP in particular) doesn't work this way. By way of comparison, I've done infinitely more development in PHP then that of VB in the recent past. Therefore, little details like this one are easy to overlook when developing with Notepad. I hate the .Net IDE and for that reason refuse to use an IDE for scripting purposes. If I wanted to write & compile a program in VB, sure, I'd be there, but not for scripts. However, in this case, I couldn't remember the keyword and didn't have the intelisense present to help me out. I finally figured it out with the assistance of Google. A quick search for "VB Script + REDIM" got me my answer. Once I got the answer, the script worked like a charm.

I hate forgetting stuff that was at one time ingrained in my mind. Well, such is life.



3 comments:

J Beagle said...

I'm not a fan of the .Net IDE. I've dabbled a bit on C# in it and it doesn't seem to be all that useful. MS SQL server 2005 uses it for its server management as well so its definitely something MS is pushing.

I stick with jEdit for all my coding needs.

Unknown said...

I have no idea what you just said, can I have whatever dictionary is needed to translate that.

Gigawatz said...

Dude, don't worry about. You're not a developer and I don't think you've ever programmed anything in your life (expect maybe a VCR, do you remember those devices?). If you got lost in this, I'm not surprised. It is ok.