Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Index outside the bounds of the array

Posted on 2010-11-09
7
Medium Priority
?
708 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
6 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 1400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Assisted Solution

by:mkobrin
mkobrin earned 600 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

876 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