VB.NET中的IndexOutOfRangeException [英] IndexOutOfRangeException in VB.NET

查看:276
本文介绍了VB.NET中的IndexOutOfRangeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

选择一个最初为.NET框架1.1版编写的旧版VB.NET项目。我运行的是带有.NET 3.5的Vista。我已经清除了所有原始错误,并且该项目将构建;它不会运行。

Picked up a legacy VB.NET project originally written for version 1.1 of the .NET framework. I'm running Vista with .NET 3.5. I have cleared out all the original error and the project will build; it just wont run.

据我所知,它正在尝试运行 LoginForm,但由于在到达断点之前就抛出了错误,所以无论如何都不能放入断点它们在文件中的放置位置。

As far as I can tell, it is trying to run 'LoginForm' but putting breakpoints in doesn't work because the error is thrown before the breakpoints are reached, regardless of where in the file they are placed.

真的不知道该怎么办!感谢所有帮助。

Really can't work out what to do! Any help appreciated.

StackTrace:

StackTrace:


System.IndexOutOfRangeException was unhandled
  Message="Index was outside the bounds of the array."
  Source="FirstLine"
  StackTrace:
       at FirstLine.LoginForm.main()
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

编辑:非常抱歉,我不欣赏代码的用处,因为问题更多,我无法理解。但是,这是主要功能:

Terribly sorry, didn't appreciate the code would be of much use because the issue is more that I can't get to it. However, here's the main function:


Shared Sub main()
        Dim p As Process() = Process.GetProcessesByName("FirstLine")
        If p.Length = 1 Then
            'START COPYMINDER
            'Dim expirydate As Date = CDate("01/01/1970")
            'Dim expiry As Integer
            'Try
            '    GetCopyMinderExpiryDate(expiry)
            '    If Not expiry = 0 And Not expiry = 1 Then
            '        expirydate = expirydate.AddSeconds(expiry)
            '        Dim diff As Integer = DateDiff(DateInterval.Day, Date.Now, expirydate)
            '        If diff >= 0 And diff  0 Then
            '    DisplayError((ret_code))
            '    End
            'End If

            'Dim did As String
            'GetCopyMinderDeveloperID(did)
            'If did  "IT" Then
            '    MessageBox.Show("Invalid Developer ID " & did & ". Firstline will now shutdown", "Firstline", MessageBoxButtons.OK, MessageBoxIcon.Error)
            '    End
            'End If

            'END COPYMINDER


            Dim lf As New LoginForm
            If LoginSettings.setting("loginShowErrorOnLine") = "TRUE" Then
                lf.ShowDialog()
            Else
                Try
                    lf.ShowDialog()
                Catch ex As Exception
                    MsgBox(ex.Message)
                    Config.UnlockByUser(Config.currentUser.username)
                    Config.currentUser.UserLoggedOff()
                End Try
            End If
        Else
            Dim prc As Process = p(0)
            SwitchToThisWindow(prc.MainWindowHandle, True)
        End If

    End Sub

感谢您到目前为止的回答。

Thanks for your responses so far. It's encouraging to see such a helpful community!

推荐答案

Dim prc As Process = p(0)是您的问题,因为else语句中的数组长度可以是1(例如0)以外的任何值。

Dim prc As Process = p(0) is your problem as it is in the else statements where the the array length can be anything but 1 (0 for instance).

为0时,当您尝试访问第一个元素时,它将为您提供IndexOutOfRange。

when the length is 0 it will give you the IndexOutOfRange when you try to access the first element.

这篇关于VB.NET中的IndexOutOfRangeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆