Results 1 to 4 of 4

Thread: Modifying an array based on a structure within a function

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    10

    Modifying an array based on a structure within a function

    Hi guys

    I've run into a bit of an issue which I'm struggling with and wonder if anyone can give me an idea of what I'm missing.

    I have an array based on a structure which in turn has three string values. I'm unable to use Replace to remove or replace a character in one of those strings (.Value) - it does nothing.
    If I manually loop through the string and look for the the character to be replaced, I can delete or replace without issue.
    I get no errors and the code continues on - just Replace has no effect.

    Any thoughts? Is this an issue due to Replace not being able to dela with the structure?


    Here's the code in question (only):

    Code:
    Public Module Module_Definitions
        Public Structure CMDlineDBstruct
            Public Value As String
            Public StartPos As Integer
            Public EndPos As Integer
        End Structure
    
        Public CMDlineDB(100) As CMDlineDBstruct
    End Module
    
    
    
    ' The following does *not* work
    
    Public Class CmdParser
        Public Sub RemoveChrFromCMDlineDB(c As String)
            For i As Integer = 1 To CMDlineCount
                Replace(CMDlineDB(i).Value, c, "")
            Next
        End Sub
    End Class
    
    
    ' Where-as the following works
    Public Class CmdParser
        Public Sub t(c As String)
            For i As Integer = 1 To CMDlineCount
                For k As Integer = 1 To Len(CMDlineDB(i).Value)
                    If Mid(CMDlineDB(i).Value, k, 1) = c Then CMDlineDB(i).Value = Left(CMDlineDB(i).Value, k - 1) & Mid(CMDlineDB(i).Value, k + 1)
                Next k
            Next i
        End Sub
    End Class

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Modifying an array based on a structure within a function

    Did you read the documentation for Replace?

    https://docs.microsoft.com/en-us/dot...e?view=net-5.0

    "Returns a new string ..."

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    10

    Re: Modifying an array based on a structure within a function

    Quote Originally Posted by OptionBase1 View Post
    Did you read the documentation for Replace?

    https://docs.microsoft.com/en-us/dot...e?view=net-5.0

    "Returns a new string ..."
    You are correct and it now works. That's annoying .
    Just to explain: I honestly tried that at the beginning and it didn't seem to work, but there was probably something else wrong with the calling routine and I wasn't thorough enough at the time.
    Also, there is a version of Replace which works in the way I was trying in VBA so I wonder if I saw that at some point.

    Anyway, thanks for the pointer. I feel a tad silly. .

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,467

    Re: Modifying an array based on a structure within a function

    There is the .Net String.Replace function, and also the legacy Replace function...

Tags for this Thread

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