Why I can't run it in DOS command line?

jwu1023

Member
Joined
Jan 9, 2006
Messages
13
Location
Memphis, TN
Programming Experience
3-5
Dear all,

I created a module and a form in Visual Studio.Net like following:

VB.NET:
Module Module1
Public sum AsInteger
PublicSub Main(ByVal args() AsString)
Dim sum AsInteger
For i AsInteger = 0 To UBound(args)
sum += CInt(args(i))
Console.WriteLine(args(i))
Next
System.Windows.Forms.Application.Run(New Form1)
EndSub
EndModule
VB.NET:
PublicClass Form1
Inherits System.Windows.Forms.Form
:
:
PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
TextBox1.Text = sum
EndSub
EndClass
However, when I ran it in DOS like:
C:\WinAPP2 10 20 30

Result: nothing happen, the Form1 pops up showing sum = 0.

Do I miss anything or the way I passed the command line parameter is not right? Thanks for your help in advance.

jwu1023
 
Last edited by a moderator:
When you do this:System.Windows.Forms.Application.Run(New Form1)

You are telling the app to re-start with the new startup form.... that means that your previous Sum value will be lost....

Unless you have a reason to restart it likethat... just simply show the form...

VB.NET:
Dim MyForm as Form1 = New Form1

MyForm.ShowDialog

-tg
 
Before showing the form, right inside of your Main sub, try displaying a MessageBox with the sum.... make sure it's even running through the calculation correctly.

-tg
 
Thanks for the reminding.
I added code "MsgBox(sum.ToString)" before definig new form1.

if I run :
C:\WinApp2 10 20 30

Result: Messagebox shows 60. But the "sum" in Textbox on the form shows 0.

It seems sum is reinitiated somewhere after.
 
Can you show what code you have now?

-tg
 
My code is:

Module Module1
Public sum As Integer
Public Sub Main(ByVal args() As String)
Dim sum As Integer
For i As Integer = 0 To UBound(args)
sum +=
CInt(args(i))
Console.WriteLine(args(i))
Next
MsgBox(sum.ToString)
Dim MyForm As Form1 = New Form1
MyForm.ShowDialog()
' System.Windows.Forms.Application.Run(New Form1)
End Sub
End
Module

Public Class Form1
Inherits System.Windows.Forms.Form
#
Region " Windows Form Designer generated code "

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = sum
End Sub
End
Class
 
from inside the form1 code window, in the left dropdown, select Form1 (but NOT Form1 Events).... then in the right dropdown, select "New" .... this will display the default constructor for form1. Copy it in its entirety.... then paste it right below.... Change the New method definition to accept a parameter.... Then inside the code for the sub, there should be a line that calls initializecomponents followed by a comment.... AFTER that comment... add code to take the parameter value and put it where you want it. Basically what you have now done is created a overloaded constructor for the form. Remove all of the Form_Load code.

Now, back in the Main sub....
Change this:
Dim MyForm As Form1 = New Form1
to this:
Dim MyForm As Form1 = New Form1(Sum)

-tg
 
Thanks a lot, tg. I works great now.

One thing I am confused is that variable "sum" is a global variable, and my project runs from "Sub Main(args())", which assigns value to "sum". How could not Form1_load not get the value of "sum"? Theoretically, could you please explain to me? Thank you very much.
 
tg, here is what I did as you suggested:

Module
Module1
Public sum As Integer
Public Sub Main(ByVal args() As String)
Dim sum As Integer
For i As Integer = 0 To UBound(args)
sum +=
CInt(args(i))
Console.WriteLine(args(i))
Next
MsgBox(sum.ToString)
Dim MyForm As Form1 = New Form1(sum)
MyForm.ShowDialog()
' System.Windows.Forms.Application.Run(New Form1)
End Sub
End
Module

Public Class Form1
Inherits System.Windows.Forms.Form
Dim formSum As Integer
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
Public Sub New(ByVal fSum As Integer)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
formSum = fSum
'Add any initialization after the InitializeComponent() call
End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = formSum
End Sub
End
Class
 
That's actualy not what I said..... it's close, just not quite... the Form_Load event was taken out.... and the setting of the text box was moved to the New constructor....

VB.NET:
Module Module1
Public sum AsInteger
PublicSub Main(ByVal args() AsString)
Dim sum AsInteger
For i AsInteger = 0 To UBound(args)
sum += CInt(args(i))
Console.WriteLine(args(i))
Next
MsgBox(sum.ToString)
Dim MyForm As Form1 = New Form1(sum)
MyForm.ShowDialog()
' System.Windows.Forms.Application.Run(New Form1)
EndSub
EndModule

PublicClass Form1
Inherits System.Windows.Forms.Form
Dim formSum AsInteger
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
EndSub

Public Sub New(ByVal fSum AsInteger)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
TextBox1.Text = fSum
EndSub

#EndRegion

EndClass

-tg
 
Back
Top