I am trying to port this code from VBScript to C#
-
Unfortunately I am not familiar with any languages with a "V" in name...
Function zGetFolder( sFolder )
On Error Resume Next
Set zGetFolder = oFso.GetFolder( sFolder )
If Err.Number <> 0 Then
Wscript.Echo "Error connecting to: " & sFolder & VBlf & "[" & Err.Number & "]" & Err.Description
Wscript.Quit Err.Number
End If
End FunctionQuestions: The line I don't understand is: Set zGetFolder = oFso.GetFolder(sFolder) Why would you "Set" a call to oFso.GetFolder with "zGetFolder" the name of the function you are currently in??? I am also not quite sure what "On Error Resume Next" accomplishes with this construct.
-
Unfortunately I am not familiar with any languages with a "V" in name...
Function zGetFolder( sFolder )
On Error Resume Next
Set zGetFolder = oFso.GetFolder( sFolder )
If Err.Number <> 0 Then
Wscript.Echo "Error connecting to: " & sFolder & VBlf & "[" & Err.Number & "]" & Err.Description
Wscript.Quit Err.Number
End If
End FunctionQuestions: The line I don't understand is: Set zGetFolder = oFso.GetFolder(sFolder) Why would you "Set" a call to oFso.GetFolder with "zGetFolder" the name of the function you are currently in??? I am also not quite sure what "On Error Resume Next" accomplishes with this construct.
In VBScript, if a function returns an instance of a class or any object, you have to SET the variable to that instance. Also, "On Error Resume Next" is error handling, which would be a try/catch block in C#. Resume Next just tells VB to continue executing code even if an error occurs. It also sets the values of the Err object if an error does occur. Basically, all that code boils down to this in C#:
var zGetFolder = new DirectoryInfo(sFolder);
You can find the documentation on the DirectoryInfo class here[^].
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
In VBScript, if a function returns an instance of a class or any object, you have to SET the variable to that instance. Also, "On Error Resume Next" is error handling, which would be a try/catch block in C#. Resume Next just tells VB to continue executing code even if an error occurs. It also sets the values of the Err object if an error does occur. Basically, all that code boils down to this in C#:
var zGetFolder = new DirectoryInfo(sFolder);
You can find the documentation on the DirectoryInfo class here[^].
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakThanks for the explanation. What you said makes perfect sense. I still don't understand why you would do this: Set zGetFolder = oFso.GetFolder( sFolder ) When zGetFolder is the name of the function that line of code is in. Does this zGetFolder only exist in the scope of the function?
-
Thanks for the explanation. What you said makes perfect sense. I still don't understand why you would do this: Set zGetFolder = oFso.GetFolder( sFolder ) When zGetFolder is the name of the function that line of code is in. Does this zGetFolder only exist in the scope of the function?
I think you'll find that the function is designed to return the folder to the calling method. Therefore
Steve Messer wrote:
Set zGetFolder = oFso.GetFolder( sFolder )
should return the folder (it will be a variable in the calling function).
Never underestimate the power of human stupidity RAH
-
Thanks for the explanation. What you said makes perfect sense. I still don't understand why you would do this: Set zGetFolder = oFso.GetFolder( sFolder ) When zGetFolder is the name of the function that line of code is in. Does this zGetFolder only exist in the scope of the function?
-
Thanks for the explanation. What you said makes perfect sense. I still don't understand why you would do this: Set zGetFolder = oFso.GetFolder( sFolder ) When zGetFolder is the name of the function that line of code is in. Does this zGetFolder only exist in the scope of the function?
Steve Messer wrote:
When zGetFolder is the name of the function that line of code is in.
That is how a function returns data to its caller. The name of the function is an alias for the name of the variable to return. So setting the name in that way, the caller of the function will receive the value obtained by
GetFolder
. -
Thanks for the explanation. What you said makes perfect sense. I still don't understand why you would do this: Set zGetFolder = oFso.GetFolder( sFolder ) When zGetFolder is the name of the function that line of code is in. Does this zGetFolder only exist in the scope of the function?
The GetFolder-Method comes from oFso. This must be declared somewhere else and means the FileSystemObject (Example : Set FSO = CreateObject("Scripting.FileSystemObject") ). If you come from VBScript you will find a lot of differences (not only with the Code-Syntax) to .Net (it is independant if you take VB.NET or C#.Net).
-
In VBScript, if a function returns an instance of a class or any object, you have to SET the variable to that instance. Also, "On Error Resume Next" is error handling, which would be a try/catch block in C#. Resume Next just tells VB to continue executing code even if an error occurs. It also sets the values of the Err object if an error does occur. Basically, all that code boils down to this in C#:
var zGetFolder = new DirectoryInfo(sFolder);
You can find the documentation on the DirectoryInfo class here[^].
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakDave Kreskowiak wrote:
Basically, all that code boils down to this in C#:
Almost. :) The GetFolder Method[^] generates an error if the folder doesn't exist. The
DirectoryInfo
constructor doesn't. You'd need to check theExists
property, and either display a message or throw an exception if it returnsFalse
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Dave Kreskowiak wrote:
Basically, all that code boils down to this in C#:
Almost. :) The GetFolder Method[^] generates an error if the folder doesn't exist. The
DirectoryInfo
constructor doesn't. You'd need to check theExists
property, and either display a message or throw an exception if it returnsFalse
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Yeah, I know. There are other issues with the OP's understanding that I just didn't directly address because of time.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
Thanks for the explanation. What you said makes perfect sense. I still don't understand why you would do this: Set zGetFolder = oFso.GetFolder( sFolder ) When zGetFolder is the name of the function that line of code is in. Does this zGetFolder only exist in the scope of the function?
zGetFolder
is NOT the name of a function. It's just a variable holding a value or, in this case, and object returned by the call to the GetFolder() function. The GetFolder() function is a member of the FileSystemObject class instance that the variableoFso
holds.Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
zGetFolder
is NOT the name of a function. It's just a variable holding a value or, in this case, and object returned by the call to the GetFolder() function. The GetFolder() function is a member of the FileSystemObject class instance that the variableoFso
holds.Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakSo basically: 1. 'On Error Resume Next' turns on user error handling as the default behavior is to exit on errors 2. If the call to oFso.GetFolder returns an error the function will Echo out the error code and continue execution instead of the normal behavior of exiting This would make since as this is part of an ETL process which is just copying backups to a shared drive. The process doesn't need to stop if the shared drive is unavailable for some reason. Thanks everyone!
-
So basically: 1. 'On Error Resume Next' turns on user error handling as the default behavior is to exit on errors 2. If the call to oFso.GetFolder returns an error the function will Echo out the error code and continue execution instead of the normal behavior of exiting This would make since as this is part of an ETL process which is just copying backups to a shared drive. The process doesn't need to stop if the shared drive is unavailable for some reason. Thanks everyone!
Steve Messer wrote:
the function will Echo out the error code and continue execution instead of the normal behavior of exiting
Not quite. The
Wscript.Quit Err.Number
line following theWscript.Echo ...
line will cause the script to quit, with no possibility for error handling code further up the stack to handle the error.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Steve Messer wrote:
the function will Echo out the error code and continue execution instead of the normal behavior of exiting
Not quite. The
Wscript.Quit Err.Number
line following theWscript.Echo ...
line will cause the script to quit, with no possibility for error handling code further up the stack to handle the error.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I didn't notice Wscript.Quit, In my mind I just saw to lines with Echo. That takes all the mysteries out. Thanks again!!
-
Unfortunately I am not familiar with any languages with a "V" in name...
Function zGetFolder( sFolder )
On Error Resume Next
Set zGetFolder = oFso.GetFolder( sFolder )
If Err.Number <> 0 Then
Wscript.Echo "Error connecting to: " & sFolder & VBlf & "[" & Err.Number & "]" & Err.Description
Wscript.Quit Err.Number
End If
End FunctionQuestions: The line I don't understand is: Set zGetFolder = oFso.GetFolder(sFolder) Why would you "Set" a call to oFso.GetFolder with "zGetFolder" the name of the function you are currently in??? I am also not quite sure what "On Error Resume Next" accomplishes with this construct.