Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old October 4th, 2008, 09:02 PM
Newbie
 
Join Date: Oct 2008
Posts: 7
Default ASP (in VBScript) array problem - type mismatch

Greetings,
This is my first post here but I've been reading here a while and have developed a bit of a problem I thought I'd seek some advice on.

As the subject suggests, I'm building a page with ASP and using VBScript as my language. I'm relatively new to programming in general so forgive me if I'm way off base here somewhere.

What I'm doing is reading file names from a directory on my server and storing them as one long string in a variable. The filenames are actually a relative path like "/folder/filename.jpg." These are being brought in with the FileSystemObject and loaded in to the variable with a loop.
Expand|Select|Wrap|Line Numbers
  1.     for each item in folder.Files
  2.         url = MapURL(item.path)
  3.  
  4.         ' store the file names
  5.         if fileList = NULL Then
  6.             fileList = url & ","
  7.         else
  8.             fileList = fileList & url & ","
  9.         end if
  10.     next
  11.  
Then, I try to write the contents out...

Expand|Select|Wrap|Line Numbers
  1.     response.write(fileList)
  2.     response.write("<br><br>")
  3.  
  4.     newList = split(fileList,",")
  5.  
  6.     for each i in newList
  7.         response.write(newList(i) & "<br>") '<--- this line throws the error
  8.     next
  9.  
Now, in researching this up to this point (even on these very forums), I learned that when you split a variable like I've done to fileList, newList becomes an array of everything separated by a "," in fileList.

But, when I try to write the values saved in the array I get an error "Type mismatch: '/testfolder/testimg.jpg'"

I've tried quite a bit but obviously haven't been able to get it working. What am I missing here?

Not that I think it matters for this particular issue, but I'm running IIS on XP Pro.

Thanks in advance!

Edit --> response.write(fileList) prints all the relative paths just fine just like the code looks like it should.

/folder/testimage1.jpg,/folder/testimage2.jpg,/folder/testimage3.jpg,/folder/testimage4.jpg and on and on.
Reply
  #2  
Old October 5th, 2008, 04:46 AM
Newbie
 
Join Date: Oct 2008
Posts: 7
Default

Actually... nevermind. I took a break and when I came back I realized my problem was a fundamental misunderstanding of looping through an array in VBScript.

Unlike in C++ where you would say something like...

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < n; i++) {
  2.      cout << myArray[i] << endl;
  3. }
  4.  
I was following that same style logic with this and that failed to accomplish the goal.

Instead, I changed my loop to...

Expand|Select|Wrap|Line Numbers
  1.     for each i in newList
  2.         response.write(i & "<br>")
  3.     next
  4.  
Anyway, just thought I'd put my solution up in the public as well in the event other beginners were running in to the same type of problem.

Thanks for your consideration. I'm sure I'll be back.

- John
Reply
  #3  
Old October 6th, 2008, 08:31 AM
DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 924
Default

Hi John,

I'm glad you fixed your problem and thanks for posting the solution.

Oh and welcome to Bytes!

Dr B
Reply
  #4  
Old October 6th, 2008, 06:04 PM
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Age: 32
Posts: 2,374
Default

Quote:
Originally Posted by yandhi
Actually... nevermind. I took a break and when I came back I realized my problem was a fundamental misunderstanding of looping through an array in VBScript.

Unlike in C++ where you would say something like...

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < n; i++) {
  2.      cout << myArray[i] << endl;
  3. }
  4.  
I was following that same style logic with this and that failed to accomplish the goal.

Instead, I changed my loop to...

Expand|Select|Wrap|Line Numbers
  1.     for each i in newList
  2.         response.write(i & "<br>")
  3.     next
  4.  
Anyway, just thought I'd put my solution up in the public as well in the event other beginners were running in to the same type of problem.

Thanks for your consideration. I'm sure I'll be back.

- John
Right, because if you say "for each i in newList" you make i be each of the elements, not the index of the elements. To do a similar thing to what you did in C++ you would say "for i = 0 to ubound(newList)". Anyway, glad you found your solution.

Jared
Reply
  #5  
Old October 7th, 2008, 01:36 AM
Newbie
 
Join Date: Oct 2008
Posts: 7
Default

Ahh awesome, thanks Jared. Much appreciated. I'll definitely keep that in mind.
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles