Solved

Index outside the bounds of the array

Posted on 2010-11-09
7
677 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 …
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

21 Experts available now in Live!

Get 1:1 Help Now