473,387 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Can anyone explain this behavior regarding string variable passing

TheSmileyCoder
2,322 Expert Mod 2GB
I have this code:
Expand|Select|Wrap|Line Numbers
  1. Public Function SpecialFolderPath(strFolder As String) As String
  2.  
  3.     Dim objWSHShell As Object
  4.     Dim strSpecialFolderPath
  5.  
  6.     'On Error GoTo ErrorHandler
  7.     ' Create a shell object
  8.     Set objWSHShell = CreateObject("WScript.Shell")
  9.     ' Find out the path to the passed special folder,
  10.     '  just change the "Desktop" for one of the other options
  11.     SpecialFolderPath = objWSHShell.SpecialFolders(strFolder)
  12.  
  13.     ' Clean up
  14.     Set objWSHShell = Nothing
  15.     Exit Function
  16. ErrorHandler:
  17.  
  18.     MsgBox "Error finding " & strSpecialFolder, vbCritical + vbOKOnly, "Error"
  19. End Function
Now regardless of what I pass as argument (strFolder) I always get the same result:
C:\WINNT\Profiles\All Users\Desktop
Now If I change line 11 of the code to:
Expand|Select|Wrap|Line Numbers
  1. SpecialFolderPath = objWSHShell.SpecialFolders(strFolder & "")
I get the correct return value, depending on what I pass as strFolder argument.

Do anyone have a good explanation of why that is?
Mar 2 '11 #1

✓ answered by Lysander

I could be really flash here and pretend I worked this out all by myself, but I cheated and googled the problem:)

I think that the reason for the 2 different results is that when you are doing:
wshshell.SpecialFolders(FolderString)
you are really pulling up the 'item' property.

wshshell.SpecialFolders.item(FolderString)

The argument in the 'item' property is a variant (legal values can be either a number or a string). When you send just a plain string variable it doesn't know what to do with it so it defaults to the 'desktop' special folder.
The following change to your code seems to work

Expand|Select|Wrap|Line Numbers
  1. SpecialFolderPath = objWSHShell.SpecialFolders.Item(CVar(strFolder))
Again from another site
For whatever odd reason it seems to try to evaluate the String value passed as an Integer, and gets 0.

I think the fact that this a not a function but a property returning a collection object confuses us when we look at it too.

Very disconcerting behavior all right.
All credit to dilettante and mjclare (whoever they are) for a post on another site in 2007

7 2370
NeoPa
32,556 Expert Mod 16PB
Appending an empty string has the effect of coercing a value to a string type. That's the only effect I can see. As the variable is already clearly declared as a string anyway, this effect does surprise me Smiley. To explore further I'd need more information on both the data being passed, as well as the code used to call the function. It's almost as though the calling code can somehow confuse the function into dealing with value that isn't a valid string. This is certainly a weird one on the face of it.

An attached database may make looking even easier. You know the drill with that, I'm sure, but for ease of reference I'll copy it below anyway.

When attaching your work please follow the following steps first :
  1. Remove anything not relevant to the problem. This is not necessary in all circumstances but some databases can be very bulky and some things do not effect the actual problem at all.
  2. Likewise, not entirely necessary in all cases, but consider saving your database in a version not later than 2003 as many of our experts don't use Access 2007. Largely they don't want to, but some also don't have access to it. Personally I will wait until I'm forced to before using it.
  3. If the process depends on any linked tables then make local copies in your database to replace the linked tables.
  4. If the database includes any code, ensure that all modules are set to Option Explicit (See Require Variable Declaration).
  5. If you've done anything in steps 1 to 4 then make sure that the problem you're experiencing is still evident in the updated version.
  6. Compile the database (From the Visual Basic Editor select Debug / Compile {Project Name}).
  7. Compact the database (Tools / Database Utilities / Compact and Repair Database...).
  8. Compress the database into a ZIP file.
  9. When posting, scroll down the page and select Manage Attachments (Pressing on that leads you to a page where you can add or remove your attachments. It also lists the maximum file sizes for each of the allowed file types.) and add this new ZIP file.
It's also a good idea to include some instructions that enable us to find the issue you'd like help with. Maybe some instructions of what to select, click on, enter etc that ensures we'll see what you see and have the same problems.
Mar 2 '11 #2
TheSmileyCoder
2,322 Expert Mod 2GB
Hi NeoPa and thank you for your reply.

I have uploaded a simple database designed to visualize the problem.

I remember once before having run into a similar situation, but can't remember where it was :P
Attached Files
File Type: zip SpecialFolderExample.zip (31.6 KB, 71 views)
Mar 2 '11 #3
Rabbit
12,516 Expert Mod 8TB
That's odd because I have no problems with that code. Have you tried recreating it in a fresh database? Maybe it's just corruption.
Mar 2 '11 #4
Lysander
344 Expert 100+
I could be really flash here and pretend I worked this out all by myself, but I cheated and googled the problem:)

I think that the reason for the 2 different results is that when you are doing:
wshshell.SpecialFolders(FolderString)
you are really pulling up the 'item' property.

wshshell.SpecialFolders.item(FolderString)

The argument in the 'item' property is a variant (legal values can be either a number or a string). When you send just a plain string variable it doesn't know what to do with it so it defaults to the 'desktop' special folder.
The following change to your code seems to work

Expand|Select|Wrap|Line Numbers
  1. SpecialFolderPath = objWSHShell.SpecialFolders.Item(CVar(strFolder))
Again from another site
For whatever odd reason it seems to try to evaluate the String value passed as an Integer, and gets 0.

I think the fact that this a not a function but a property returning a collection object confuses us when we look at it too.

Very disconcerting behavior all right.
All credit to dilettante and mjclare (whoever they are) for a post on another site in 2007
Mar 2 '11 #5
NeoPa
32,556 Expert Mod 16PB
Wow! I didn't expect this to be a WSH (Windows Scripting Host) issue. Nice find Lysander.

I did a fair bit of scripting, particularly with AD (Active Directory) and the Registry, a few years back and I do recall that due to the flexible nature of values (Not only numeric versus string but also arrays and scalars, and even single entry arrays - just to make life ultra-complicated), it was necessary for me to write routines to set and get the values using a routine that determined the required type and converted to and from that type before use. All good fun.

Unfortunately, I no longer have access to any of that work, and it's been a couple of years since I did it so my recollections are hazy. I must visit my old workplace sometime and get the code I developed at least. It may not be any use to me directly, but it will at least provide me with reminders of good ways to approach things.

I was also impressed yet again Smiley, that you provided a properly prepared database with which to illustrate your problem. I probably won't need to do anything with it now, but I have a copy still, so can go back to it at need.
Mar 3 '11 #6
TheSmileyCoder
2,322 Expert Mod 2GB
@Rabbit: The attached example IS a fresh db, created just for demo purposes.

@Lysander: Thank you for finding that for me. I am not 100% into the exact mechanics of using variants, since in 99.9% of cases I declare my variables as strings, or integers/longs/doubles. I am still surprised that the interpretation of a variant can lead to such issues. Anyways thank you.

@Neopa: After answering a question here and there, you get to the point where you properly realise how important the specifics of a question can be, especially one thats a bit more complicated then the average.
Mar 6 '11 #7
NeoPa
32,556 Expert Mod 16PB
That's so true Smiley, not that your contributions could easily be dismissed as merely answering a question here and there mind.
Mar 7 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Rigga | last post by:
Hi, Ok first please bear with me as I am a total Python n00b.. Can anyone explain why this does not like me using % FileLoc in the os.system call??? #!/usr/bin/python import sys import os
17
by: matthias_k | last post by:
Hi, I am currently implementing a solver for linear optimization problems using the revised simplex method and I stumbled accross some strange behavior regarding the treatment of the number 0....
2
by: Mike Moore | last post by:
does anyone have an example of how to get the connection string object converted to a string variable type in order for me to call a function?
0
by: shaikm | last post by:
Hi, I have problem passing variable in a FOR LOOP. Following is my code. Any help much appreciated. ZS -- Procedure teta has two parameters l and q , l is the feature table and q is the...
1
by: Jia Lu | last post by:
Hello I have a program that can telnet to a host. But I cannot understand from part, can anyone explain it to me? Thank you very much. import sys, posix, time
3
by: shawrie | last post by:
Hello everyone can anyone please help me? I basically want to set the length of a string variable to help spacing my simple report out. i tried dim test as string(14) but it didnt like that
9
by: JohnR | last post by:
I have the name of a control in a string variable and I want to change one of the controls properties. Right now I recursively scan all the controls on the form until I get one whose name matches...
2
by: tonymcq | last post by:
I am very new to VB.net / ASP and have spent hours and hours trying to solve what must be a simple problem - hopefully someone can point me in the right direction. I have a gridview bound to an...
1
by: Sanny` | last post by:
I have many datareports in my project.I need to pass the datareport name as a string variable so that i will be able to open the appropriate database.Pls help
0
by: sama123 | last post by:
Is it possible to save execute stored proc command in a string variable and then execute string variable??? Does this logic fits into LINQ2SQL? Why I want to do this - INstead of passing user...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.