Results 1 to 4 of 4

Thread: For Loop Debug

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    14

    For Loop Debug

    I was making an invalid character removal module for my program, and here is the function that I used:
    Code:
    Function fix_extensions(file_extension As String)
            'Fixes file extensions
            Dim banned_ext_chars() As String = {"\", "/", ":", ";", "*", "?", """", "<", ">", "|", "%", ",", "#", "$", "!", "+", "{", "}", "&", "[", "]", "•", "'", "."}
            For index As Integer = 1 To banned_ext_chars.Length - 1
                file_extension = file_extension.Replace(banned_ext_chars(index), "")
            Next
            file_extension = "." & file_extension
            Return file_extension
        End Function
    However, upon checking the output through a short debug, it appears that the backslash was never filtered out. I've tried many things, but none worked.

    Help would be greatly appreciated.

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: For Loop Debug

    Hi,

    All collections in VB.NET, including Arrays, are indexed from Zero. Therefore your For Loop is incorrect. It should be:-

    Code:
    For index As Integer = 0 To banned_ext_chars.Length - 1
    Hope that helps.

    Cheers,

    Ian

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    14

    Re: For Loop Debug

    Ah, yes. Of course. Just a little carelessness...

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: For Loop Debug

    Code:
    For index As Integer = 1 To banned_ext_chars.Length - 1
    What's the index of the backslash in your array of banned characters?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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