cleopard Level: Big Cheese Registered: 02-09-2005 Posts: 28
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.
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
|
cleopard Level: Big Cheese Registered: 02-09-2005 Posts: 28
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.