Solved

Index outside the bounds of the array

Posted on 2010-11-09
7
679 Views
Last Modified: 2012-05-10
Hi experts!

Using this:

Public Sub LoadEnglish()
        Dim args() As String = Environment.GetCommandLineArgs
        Dim Enfi As New System.IO.FileInfo(args(1))
        If args.GetUpperBound(0) >= 1 Then
            Try
                If Enfi.Exists AndAlso Enfi.Extension.ToUpper = ".JMTE" Then
                    TextBoxEN.Text = System.IO.File.ReadAllText(args(1))
                End If
            Catch ex As Exception
                MessageBox.Show("File: " & args(1) & vbCrLf & vbCrLf & ex.ToString, "Error Opening File", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
 End Sub

Open in new window


And putting " LoadEnglish()" inside my form load, I get my app to start when the user double clicks on a ".jmte" file and load the text into a text box for editing.

The problem I have is that when the app is started via the shortcut (without passing a file name as argument) I get this error message:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at Translator_Helper.frmMain.LoadEnglish()
   at Translator_Helper.frmMain.frmMain_Load(Object sender, EventArgs e)


How can i avoid this?

Please help.
0
Comment
Question by:jorgemgonzalez
7 Comments
 

Expert Comment

by:Phani Kiran Chikkala
ID: 34099837
first of all you need to ensure that arguments are passed from commandline

Dim Enfi As New System.IO.FileInfo(args(1))

prior to this statement you need to check whether the arguments are given to the program from command line
0
 
LVL 10

Accepted Solution

by:
hosneylk earned 350 total points
ID: 34099866

Public Sub LoadEnglish()

	Dim args() As String = Environment.GetCommandLineArgs

	If args.Length >= 1 Then

		Dim Enfi As New System.IO.FileInfo(args(1))

		Try

			If Enfi.Exists AndAlso Enfi.Extension.ToUpper = ".JMTE" Then

				TextBoxEN.Text = System.IO.File.ReadAllText(args(1))

			End If

		Catch ex As Exception

			MessageBox.Show("File: " & args(1) & vbCrLf & vbCrLf & ex.ToString, "Error Opening File", MessageBoxButtons.OK, MessageBoxIcon.Error)

		End Try

	End If

 End Sub

Open in new window

0
 
LVL 10

Expert Comment

by:hosneylk
ID: 34099870
oops.. it should be
If args.Length > 1 Then

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Assisted Solution

by:mkobrin
mkobrin earned 150 total points
ID: 34099894
Your check is checking if there is one or more arguments supplied, which is fine, but when you are reading the element of the array you are checking for the second item which does not necessarily exist. Remember that the array is 0 based, so if your are looking for the first element you need to read args(0). If you want to to read args(1) then as hosneylk has said, you need to check that your args array contains more than 1 element, by changine this line:
If args.Length >= 1 Then
to:
If args.Length > 1 Then
0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 34101550
Before use the args variable check for not of arguments to application using the Length method.
0
 

Author Closing Comment

by:jorgemgonzalez
ID: 34101954
That did it, thanks to all the experts for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now