Solved

Index outside the bounds of the array

Posted on 2010-11-09
7
691 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
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

Technology Partners: 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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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