Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Index outside the bounds of the array

Posted on 2010-11-09
7
Medium Priority
?
701 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 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

Independent Software Vendors: 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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

664 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