borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB Home | News Home | Forum Home | Downloads | Register | Search | PM | Profile

Previous Topic (string slices)Next Topic (How to remove duplicate records through code) New Topic Post Reply
AndreaVB OnLine : VB General : array initialization
Poster Resource
cleopard
Level: Big Cheese

Registered: 02-09-2005
Posts: 28
icon array initialization

This will sound like a pretty simple question, but I sure can't seem to find the answer.  All I want to do is take the string out of a text box and since this string will end up being used as a filename, I want to test each character of the string to see if it's one of these invalid characters.  So I want to set up an array of these invalid characters, but I sure can't seem to find what the syntax would be.  I started out trying something like this:

dim invalid_filename_chars(10) as characters

and then (since there doesn't appear to be a way to initialize that string in the declaration line) assigning the individual array elements.  But I'm not sure how to express an individual character (usually like 'a', '?', etc, with single quotes) since a single quote is used for a comment line.  Any help would be greatly appreciated.

12-12-2006 at 02:38 PM
View Profile Send Email to User Show All Posts | Add Comment
admin
Level: Administrator


Registered: 04-04-2002
Posts: 530
icon Re: array initialization

try this code:


Public Function CheckFileName(strFileName As String) As Boolean
    Dim InvalidChars As String
    Dim mChar As String * 1 'this is how we define a character in VB
    Dim i As Integer
    
    InvalidChars = "*,.\/" 'just an example
    For i = 1 To Len(InvalidChars)
        mChar = Mid(InvalidChars, i, 1) 'extract one character
        If InStr(strFileName, mChar) > 0 Then
            'our string contains the invalid character
            'so exit with false from the function
            Exit Function
        End If
    Next
    'if no invalid characters have been found exit with true
    CheckFileName = True
End Function


in VB6 a character is defined as a String*1, and an array of characters can be considered a s a string, to extract a single character from a string you can use the Mid function

____________________________
AndreaVB

12-12-2006 at 03:39 PM
View Profile Send Email to User Show All Posts Visit Homepage | Add Comment
cleopard
Level: Big Cheese

Registered: 02-09-2005
Posts: 28
icon Re: array initialization

Thanks, Admin, that did it.  It had trouble at first, but then I saw that Mid is one-based not zero-based (had my loop starting at zero) and it worked just fine.

12-12-2006 at 06:48 PM
View Profile Send Email to User Show All Posts | Add Comment
AndreaVB OnLine : VB General : array initialization
Previous Topic (string slices)Next Topic (How to remove duplicate records through code)New Topic Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder