Results 1 to 3 of 3

Thread: Need help returning arrays from functions.

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2000
    Location
    Colorado
    Posts
    39
    Is there a method for finding characters in strings?

    What I need is a method or function that will return an array that contains a list of the positions of the characters in the string. Here is the code that I have written so far. This code exists in a module and is referenced by several forms. As of right now, when I hit play, my form gives me an error that the subscript is out of range. I know that the ChrPlace() variable is being loaded and it is declared in the module as the following:

    Public ChrPlace() as integer

    Public Function ParseString4aChar(Stringtxt As String, Chr As String, NumChr As Integer)

    'This function will parse a string for a character and will return an array that contains
    ' the positions of the character in the string. The Numchr variable allows you to specify
    'how many of the characters you expect in the string. If this variable is not known
    'then specify 0.

    Dim i, ChrPlace(), ChrCount As Integer
    Dim tempchar As String
    ReDim ChrPlace(NumChr - 1)
    ChrCount = 0

    'Scroll through each char
    For i = 1 To Len(Stringtxt)
    tempchar = Mid(Stringtxt, i, 1)
    'Test to see if tempchar = char we are looking for
    If tempchar = Chr Then
    'if so increase index of array and test to see
    'if all the characters have been found
    ChrPlace(ChrCount) = i
    ChrCount = ChrCount + 1
    If (ChrCount = NumChr) And (Numchr <> 0) Then
    Exit For
    End If
    End If
    Next i
    'Return the array
    ParseString4aChar = ChrPlace()
    End Function

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    If you have VB6 then you can return an array:
    Code:
    Private Function GetCharactersPos(pText As String, pChar As String) As Integer()
        Dim arrTemp() As Integer
        Dim i As Integer
        Dim intPos As Integer
        
        intPos = InStr(pText, pChar)
        Do While intPos > 0
            ReDim Preserve arrTemp(i)
            arrTemp(i) = InStr(intPos, pText, pChar)
            i = i + 1
            intPos = InStr(intPos + 1, pText, pChar)
        Loop
        GetCharactersPos = arrTemp
    End Function
    Then you can use this function like this:
    Code:
    Private Sub Command1_Click()
      Dim strText As String
      Dim arrChars() As Integer
      
      strText = "this is a test"
      arrChars = GetCharactersPos(strText, "s")
    End Sub
    arrChars now holds 3 elements with the position of each "s", which would be 4, 7, 13.

    If you have VB5 and earlier, then you can pass an array to that function. The rest would be very similar.

  3. #3
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    What Serge mentioned at the end of his post is the simplest way of doing it. In any version of VB arrays are always passed by reference, meaning that anything you do to the array in your function will affect the original array that you passed in.
    ie:

    Public Function ParseString4aChar(ChrPlace() As Integer , Stringtxt As String, Chr As String, NumChr As Integer)


    This way you can modify your array within the function and is automatically changed.


    On a side note, I noticed that you declared your array with Public scope originally. This means any changes you make to the array, anywhere in code, will affect the array. Meaning that there is no need to pass the array in either direction (this is a bad coding convention though).

    [Edited by SonGouki on 04-03-2000 at 01:31 PM]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width