locked
What is causing 'Index was outside the bounds of the array' error? RRS feed

  • Question

  • User695372294 posted

    I have a VB.NET function on my website (derived from code I found at http://weblogs.asp.net/rmclaws/archive/2006/10/16/Get-Initials-from-a-String-in-VB.NET.aspx) that is designed to generate initials from a name. All the names I have tested work fine. However, when I run this function in a loop that processes every name in a table and generates the relevant initials for each record, I get an 'Index was outside the bounds of the array' error. I don't yet know which record in the table is triggering the error. None of the names are blank or null. 

    Two different versions of the GetInitialsFromString function are shown below, both of which generate the error, but I can't see why either version should care what string it processes. Does anyone have any suggestions?

    I also list below the Linq to SQL code I used to process the names in my Entrants table in case that is the source of my problem. It's midnight here in the UK and my tired brain has probably done something stupid...

    David

    ============================================================

            Public Shared Function GetInitialsFromString(ByVal FullName As String) As String           
    	    Dim NameArray As String() = FullName.Split(" ")
                Dim initials As String = String.Empty
                
    	     If NameArray.Length > 0 Then
                    Dim i As Integer = 0
                    Do Until i > NameArray.Length - 1
    			initials += NameArray(i).Chars(0)
    			i = i + 1
    		Loop
                End If
    
                Return initials.ToUpper()
            End Function
    
    
            Public Shared Function GetInitialsFromString(ByVal FullName As String) As String            
                Dim NameArray As String() = FullName.Split(" ")
                Dim initials As String = String.Empty
                
                For Each name As String In NameArray
                    initials += name.Chars(0)
                Next
    
                Return initials.ToUpper()
            End Function
    

    ============================================================

            Using myDBC As CompDataSQLDataContext = New CompDataSQLDataContext()
                Dim AllEntrants = From eee In myDBC.Entrants Select eee
    
                For Each ent In AllEntrants
                    ent.Initials = GetInitialsFromString(ent.FirstName)
                Next
    
                myDBC.SubmitChanges()
            End Using



     

    Saturday, January 12, 2013 7:03 PM

Answers

  • User1546878023 posted

    Your problem could be something along the line that someone has a fullname that either starts or ends with a space, such as "Peter "

    VB is not my preferred language but I expect that you could say something like FullName.Trim().Split(" ") and as a failsafe in you loop test for i.e. that name is not null or empty

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 12, 2013 7:53 PM

All replies

  • User197322208 posted

    I get an 'Index was outside the bounds of the array' error.
    Which line?

    Saturday, January 12, 2013 7:52 PM
  • User1546878023 posted

    Your problem could be something along the line that someone has a fullname that either starts or ends with a space, such as "Peter "

    VB is not my preferred language but I expect that you could say something like FullName.Trim().Split(" ") and as a failsafe in you loop test for i.e. that name is not null or empty

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 12, 2013 7:53 PM
  • User695372294 posted

    Hi Dahla,
    Using FullName.Trim.Split(" ") didn't fix it and neither did a test for a null name within the loop. However, bypassing empty name values within the loop did the job.

    There are indeed a few records in my Entrants table which have a trailing space after the name (records that must have been created before I trimmed all data entry), so that will have been the cause.

    Many thanks for your help. It's much appreciated.

    David 

    Saturday, January 12, 2013 8:28 PM
  • User695372294 posted

    Further investigation showed that my original problem was not caused by trailing spaces (which explains why using Trim did not help).

    The error was actually triggered by people with names separated by a double space.

    David

    Sunday, January 13, 2013 8:49 AM