Subject: Illegal Assignment
Posted By: Broodmdh Post Date: 8/21/2008 7:31:50 AM
I'm trying to concatenate a string in a for loop, which sounds quite simple in principle, but I'm getting the following error:

Microsoft VBScript runtime error '800a01f5'
Illegal assignment: 'I'

This is the code that I'm running:

tmpString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
numSaltChars = 10
sSalt = ""
i = 0
for i = 1 to numSaltChars
    Randomize
    pos = Round(Rnd * Len(tmpString)) + 1
    sSalt = sSalt & tmpString.substring(pos,1)
next

I can't see what's wrong with the code.  Any help would be appreciated.

Reply By: Old Pedant Reply Date: 8/21/2008 1:18:56 PM
VBScript does *NOT* support METHODS on strings.

    tmpString.substring(pos,1)

You are trying to use a substring method, but that's only available in JavaScript and in the .NET languages (and Java, I guess).

Strings are *NOT* objects in VBScript.  They are primitive types.  So you have to use built in functions.

**********

Various other goofs in your code:

You call RANDOMIZE each time through the loop.  That will actually *DESTROY* any attempts to get good random numbers.  RANDOMIZE should be called once *PER PAGE* only.

You are using ROUND where you should use INT.  ROUND *could* give you a number that is one greater than the length of your string, so you would get an "out of bounds" error!

Finally, you set i=0 for no reason at all, since the very next line will throw away that value.

********************

SO:

CONST numSaltChars = 10
Randomize ' remember, once PER PAGE!

tmpString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
sLen = Len(tmpString)
sSalt = ""

for i = 1 to numSaltChars
    sSalt = sSalt & Mid( tmpString, 1 + INT(sLen * RND()), 1 )
next


Reply By: Broodmdh Reply Date: 8/22/2008 6:21:45 AM
Great, thanks for the help.


Go to topic 73172

Return to index page 1