| Working with value passed to an exe
|
mack

msg:4033026 | 1:31 am on Nov 28, 2009 (gmt 0) | In one of my vb.net apps I pass a value to the exe file. I use the following code to handle the paramiter received and convert it to a string within the application. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'get command peramiters Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs For i As Integer = 0 To CommandLineArgs.Count - 1 Dim myvariable As String myvariable = (CommandLineArgs(i)) Label1.Text = myvariable.Length 'only used for debug If myvariable.Length = 0 Then do something End If If myvariable.Length > 0 Then do somethign else End If Next End Sub |
| When a paramiter is sent it handles it correctly, but if no peramiter is sent it simple ignores both if statements. The calue being sent is collected and passed to a variale (dim) so the value exists, even if it has no content. By checking the variable lenghth I was hoping to be able to confirm or eny if a value had been passed. Is there somethign else I need to look into to determine if a peramiter has been passed to the exe. Does having no value passed bean that peramiter.lenghth = "0" would still not work? I can confirm the value has been sent and received. I debuged this by assigning its value to a label, it showed the correct value within the app itself. Thanks in advance. Mack.
|
mack

msg:4033065 | 4:39 am on Nov 28, 2009 (gmt 0) | As I suspected having no value, is not the same as having a value.lenghth = 0 To make it work... Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'get command peramiters Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs For i As Integer = 0 To CommandLineArgs.Count - 1 Dim myvariable As String myvariable = (CommandLineArgs(i)) Label1.Text = myvariable.Length 'only used for debug If myvariable.Length = 0 Then do something exit sub End If Next do somethign else End Sub |
| by exiting the sub I don't need a second conditional statement, and can run the code that the 2nd if statement would have contained. Mack.
|
marcel

msg:4033080 | 5:30 am on Nov 28, 2009 (gmt 0) | I prefer to check for an empty (or null) string with the following code: | If String.IsNullOrEmpty(myvariable) Then ... |
|
|
kaled

msg:4033190 | 1:20 pm on Nov 28, 2009 (gmt 0) | I have never used VB.net and I am not certain that I have understood the problem correctly, however I think there are two possibilities ... 1) The for loop is executed only if at least one command-line argument is supplied. OR 2) Is Nothing should be used as the test. I could be way off the mark. Kaled.
|
mack

msg:4033340 | 9:48 pm on Nov 28, 2009 (gmt 0) | kaled and marcel, thanks for the replies. Marcel your solution is a lot more elegant than mines. Kaled, I think you're right. Because it was empty, it wasn't entering the loop. This would also explain why there where no compiler errors, because the condition simply wasn't met. Mack.
|
|
|