Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. I am trying to port this code from VBScript to C#

I am trying to port this code from VBScript to C#

Scheduled Pinned Locked Moved Visual Basic
csharphelpquestioncareer
14 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Steve Messer

    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?

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #5

    Steve Messer wrote:

    When zGetFolder is the name of the function that line of code is in.

    No they are different. The resulting variable is named zGetFolder (leading z), but the function is named GetFolder (no leading z).

    1 Reply Last reply
    0
    • S Steve Messer

      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?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #6

      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.

      1 Reply Last reply
      0
      • S Steve Messer

        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?

        R Offline
        R Offline
        Ralf Meier
        wrote on last edited by
        #7

        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).

        1 Reply Last reply
        0
        • D 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 Kreskowiak

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #8

          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 the Exists property, and either display a message or throw an exception if it returns False.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          D 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            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 the Exists property, and either display a message or throw an exception if it returns False.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #9

            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

            1 Reply Last reply
            0
            • S Steve Messer

              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?

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #10

              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 variable oFso holds.

              Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
              Dave Kreskowiak

              S 1 Reply Last reply
              0
              • D 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 variable oFso holds.

                Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                Dave Kreskowiak

                S Offline
                S Offline
                Steve Messer
                wrote on last edited by
                #11

                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!

                Richard DeemingR 1 Reply Last reply
                0
                • S Steve Messer

                  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!

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #12

                  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 the Wscript.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

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  S 1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    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 the Wscript.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

                    S Offline
                    S Offline
                    Steve Messer
                    wrote on last edited by
                    #13

                    I didn't notice Wscript.Quit, In my mind I just saw to lines with Echo. That takes all the mysteries out. Thanks again!!

                    1 Reply Last reply
                    0
                    • S Steve Messer

                      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 Function

                      Questions: 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.

                      O Offline
                      O Offline
                      oblondel
                      wrote on last edited by
                      #14

                      Hi, I think, it's because of visual basic sripting (which is equal to VB 6) The "SET" is for instancing the fso object ofso (Filesystem object i guess) The on error resume next is a poor "try catch" implementation.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups