Results 1 to 6 of 6

Thread: How do I send a parameter to a Console Application

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2008
    Posts
    23

    How do I send a parameter to a Console Application

    I created a Console Application, with a Sub Main(Byval str As String), and when I run the debugger I get the following error:

    Error 1 No accessible 'Main' method with an appropriate signature was found in ...

    Also I'd like the solution to my problem to apply not only to the debugger, but also to the .exe file that will eventually be created.

    Also, I found the following code on the internet, but I don't understand how to send the parameter in still.

    Code:
    Public Sub Main()
     
    MsgBox Command$,vbOk, "Command Line params"
     
    End Sub
    Do I have to open cmd.exe, type: programname.exe -parameter 'Or something of that sort?

    Thanks
    Last edited by 5te4lthX; Jan 26th, 2009 at 09:43 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do I send a parameter to a Console Application

    Your code should look like this:
    vb.net Code:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.         For Each arg As String In Environment.GetCommandLineArgs()
    5.             Console.WriteLine(arg)
    6.         Next
    7.  
    8.         Console.ReadLine()
    9.     End Sub
    10.  
    11. End Module
    or this:
    vb.net Code:
    1. Module Module1
    2.  
    3.     Sub Main(ByVal args As String())
    4.         For Each arg As String In args
    5.             Console.WriteLine(arg)
    6.         Next
    7.  
    8.         Console.ReadLine()
    9.     End Sub
    10.  
    11. End Module
    If your Main method does have a parameter it MUST be a String array. You can call it what you like but the convention is to name it "args".

    Note that Environment.GetCommandLineArgs will return the application file name/path itself as the first argument, while the second method will not.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2008
    Posts
    23

    Re: How do I send a parameter to a Console Application

    After the Console.ReadLine(), the args didn't seem to be initialized. I checked it with my Debugger, and when I hovered over args, it said "length = 0".

    When the console prompted me to type something, I typed: Test

    Also, I was wondering if there is a way to start my program and send it a parameter at the same time, not send a parameter during runtime. Is this possible?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do I send a parameter to a Console Application

    The only place you can send commandline parameters to an application is on the commandline, i.e. when you execute it.

    Once your app is deployed then the user might execute it by selecting Start -> Run and typing in a commandline like "YourApp -someArg" (without quotes) or else create a desktop shortcut and add the argument to the commandline in the shortcut properties.

    If you want to test your app with commandline arguments while debugging then you have to tell the IDE to pass them when it runs your app in the debugger. To do that you open the Debug page of the project properties and populate the appropriate field.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2008
    Posts
    23

    Re: How do I send a parameter to a Console Application

    Quote Originally Posted by jmcilhinney View Post
    vb.net Code:
    1. Module Module1
    2.  
    3.     Sub Main(ByVal args As String())
    4.         For Each arg As String In args
    5.             Console.WriteLine(arg)
    6.         Next
    7.  
    8.         Console.ReadLine()
    9.     End Sub
    10.  
    11. End Module
    I'm planning on using this. I'm wondering: how can there be more element in the array of args? In cmd, if I type application.exe command1 command2, does that mean that args contains two elements: {"command1", "command2"}? Or does it mean that it contains one element: {"command1 command2"}?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do I send a parameter to a Console Application

    Quote Originally Posted by 5te4lthX View Post
    I'm planning on using this. I'm wondering: how can there be more element in the array of args? In cmd, if I type application.exe command1 command2, does that mean that args contains two elements: {"command1", "command2"}? Or does it mean that it contains one element: {"command1 command2"}?
    Could you not just try it and see?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width