Application Parameters

  • Thread starter Thread starter Sergio
  • Start date Start date
S

Sergio

I need to pass two parameter to a vb.net 2003 windows form application, is
this posible? how?
 
I need to pass two parameter to a vb.net 2003 windows form application, is
this posible? how?

Sure... You either use one of the overloaded versions of sub main:

Public Sub Main (ByVal args() As String)
Public Function Main (ByVal args() As String) As Integer

or...

You can get teh command line arguments from
System.Environment.GetCommandLineArgs or
System.Environment.CommandLine. Oh, yeah VB also has the
My.Application.CommandLineArgs property as well as the old VB Command
function. I believe that the My.Application.CommandLineArgs is the
recommended way now :)
 
Tom,
My.Application.CommandLineArgs property as well as the old VB Command
function. I believe that the My.Application.CommandLineArgs is the
recommended way now :)
\
Why?

In my idea is the My class only build to make it easier for old VB6 users.
That does in my idea not make it the recommended way.

The overloaded constructor is in my idea the normal way for this.

Cor
 
I need to pass two parameter to a vb.net 2003 windows form application, is
this posible? how?

Sergio,
You can launch your application with passing parameters using.
Generally, the parameters are passed when form's load event is fired.
So you can do;

You can get all the parameters that are passed on application startup;

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
For Each x As String In Environment.GetCommandLineArgs
MsgBox(x)
Next
End Sub

Hope this helps,

Onur
 
Tom,

\
Why?

I meant that it is recommended over the Command function. In the
documentation, it cliams that the My method is faster and more
efficeint.
In my idea is the My class only build to make it easier for old VB6 users.
That does in my idea not make it the recommended way.

The overloaded constructor is in my idea the normal way for this.

The overloaded main is the way I handle it in C#, but if your using the
application framework in VB.NET, using the Main becomes a little more
difficult. I personally would use the System.Environment properties.
 
thanks Onur,

this works great, but always display one message (even if no parameters are
passed), the path of the .exe file, for ex: "c:\myApp\myApp.exe", any ideas?

Sergio.
 
Tom,

Jay B has once written all the possibilities he knew that there were with
with Sub Main in VB.Net (I thought that he was showing 12), I thought that
Armin and Herfried are never creating a real solution without that.

However as you know, only for the record.

Cor
 
thanks Onur,

this works great, but always display one message (even if no parameters are
passed), the path of the .exe file, for ex: "c:\myApp\myApp.exe", any ideas?

Sergio.

Sergio,
Sure, it displays only your application's path if you didn't pass
additional parameters. Because first parameter refers to your
application's path, the rest are usually custom ones that you pass.

If you want to use a specific parameter, you may specify its index in
command-line argument array then use where you want(form's load on
example above). :-)

For example, if you want to get only second parameter which is your
custom parameter and its index is 1 ( zero is application's path,
remember), you may do;

' Start app like this
' c:\yourapp.exe param1 param2 param3


' To get param1
If Environment.GetCommandLineArgs().Length > 1 Then
Dim my_params() As String = Environment.GetCommandLineArgs()
MsgBox(my_params(1))
End If

Use the same logic for param2, param3... for instance.

Hope this helps,

Onur Güzel
 
Tom,

Jay B has once written all the possibilities he knew that there were with
with Sub Main in VB.Net (I thought that he was showing 12), I thought that
Armin and Herfried are never creating a real solution without that.

However as you know, only for the record.

Cor

Without access modifiers, it's something like:

Sub Main()
Sub Main(ByVal args() As String)
Function Main() As Integer
Function Main(ByVal args() As String) As Integer

Armin and Herfried probably don't use the application framework, either
:)
 
Thanks, finally i used

Environment.GetCommandLineArgs.GetValue(1)

Good alternative :)

However don't forget to put if conditional, because there may come up
situations that you need to start your app without passing your custom
parameter, then you'd better;

If Environment.GetCommandLineArgs().Length > 1 Then
MsgBox(Environment.GetCommandLineArgs().GetValue(1))
End If

Regards,

Onur
 
Back
Top