Check for valid path in class Property Let 
Author Message
 Check for valid path in class Property Let

Hi there...

My problem might turn out to be quite simple, but it's got me stumped, I
want use a certain directory path in one of my objects in order to parse the
directory path... but in the property let procedure before setting the
property, I'd like to validate this directory path... for example...

Public Property Let DirectoryPath (strValue As String)
    'Code to validate the directory path?
    strDirectoryPath  = strValue
End Property

Is there a way to validate this directory path from within the class?



Sun, 19 Oct 2003 01:49:51 GMT  
 Check for valid path in class Property Let
Hi Robbie

I believe what your looking for is the Dir$ function.

Public Property Let DirectoryPath (strValue As String)
    'Code to validate the directory path?
    If Dir$(strValue, vbDirectory) > "" then
        strDirectoryPath  = strValue
    End IF
End Property

You have to remember to test the value of DirectoryPath before to do any
other processing.

--
Dave Keighan
Everybody Wins If You Post Your Replies to the NG



Sun, 19 Oct 2003 02:06:40 GMT  
 Check for valid path in class Property Let

Quote:
> Hi Robbie

> I believe what your looking for is the Dir$ function.

> Public Property Let DirectoryPath (strValue As String)
>     'Code to validate the directory path?
>     If Dir$(strValue, vbDirectory) > "" then

Or better:-

    If Len(Dir$(strValue, vbDirectory)) > 0 then

Quote:
>         strDirectoryPath  = strValue
>     End IF
> End Property

> You have to remember to test the value of DirectoryPath before to do any
> other processing.

> --
> Dave Keighan
> Everybody Wins If You Post Your Replies to the NG



Sun, 19 Oct 2003 11:15:43 GMT  
 Check for valid path in class Property Let
Hi Dave,

Using the vbDirectory parameter with Dir() does not restrict it to
directories, but to normal files plus directories, so there has to be
second test or restriction.  For example, if strValue ends with "\" it
can't refer to normal file so the following would work:

Public Property Let DirectoryPath (strValue As String)
    'Code to validate the directory path?
    If Right(strValue, 1) <> "\" Then strValue = strValue & "\"
    If Dir$(strValue, vbDirectory) <> "" Then
        strDirectoryPath  = strValue
    End If
End Property
Another way would be:

Public Property Let DirectoryPath (strValue As String)
    'Code to validate the directory path?
    If Right(strValue, 1) <> "\" Then strValue = strValue & "\"
    If Dir$(strValue & "nul") <> "" Then
        strDirectoryPath  = strValue
    End If
End Property

Regards,

John...............

Quote:

> Hi Robbie

> I believe what your looking for is the Dir$ function.

> Public Property Let DirectoryPath (strValue As String)
>     'Code to validate the directory path?
>     If Dir$(strValue, vbDirectory) > "" then
>         strDirectoryPath  = strValue
>     End IF
> End Property

> You have to remember to test the value of DirectoryPath before to do any
> other processing.

> --
> Dave Keighan
> Everybody Wins If You Post Your Replies to the NG



Sun, 19 Oct 2003 12:24:08 GMT  
 Check for valid path in class Property Let
Hi John

Thanks for responding. You brought up a very good point, the programmer may
or may not be able to guarantee that the sting contents is a pathname
appropriate to the function to be called and it should be checked.

Quote:
> If Right(strValue, 1) <> "\" Then strValue = strValue & "\"

I did have a problem here, in that, the trailing backslash has to be removed
(instead of added) for Dir$() to work though.
I've tested it with:
MsgBox Dir$("C:\Program Files\", vbDirectory)
Returns a null string
MsgBox Dir$("C:\Program Files", vbDirectory)
Returns "Program Files"

There is also the issue of adding an else clause to the routine for
situations where this property may be set more than once in the same
instance. In that case the else would look after setting the property to a
null string if an incorrect or non-existing path was passed ... and the
programmer would trap for it :-)

--
Dave Keighan
VB - A Grey Matter Universal Gym



Sun, 19 Oct 2003 12:55:02 GMT  
 Check for valid path in class Property Let
Hi Michael

Quote:
> Or better:-
>     If Len(Dir$(strValue, vbDirectory)) > 0 then

I've read that using Len is faster/better than > "" ... gotta use it more.

Thanks

--
Dave Keighan



Sun, 19 Oct 2003 12:59:05 GMT  
 Check for valid path in class Property Let
wow.... thanks people, all of you!  All helped


Quote:
> Hi John

> Thanks for responding. You brought up a very good point, the programmer
may
> or may not be able to guarantee that the sting contents is a pathname
> appropriate to the function to be called and it should be checked.

> > If Right(strValue, 1) <> "\" Then strValue = strValue & "\"
> I did have a problem here, in that, the trailing backslash has to be
removed
> (instead of added) for Dir$() to work though.
> I've tested it with:
> MsgBox Dir$("C:\Program Files\", vbDirectory)
> Returns a null string
> MsgBox Dir$("C:\Program Files", vbDirectory)
> Returns "Program Files"

> There is also the issue of adding an else clause to the routine for
> situations where this property may be set more than once in the same
> instance. In that case the else would look after setting the property to a
> null string if an incorrect or non-existing path was passed ... and the
> programmer would trap for it :-)

> --
> Dave Keighan
> VB - A Grey Matter Universal Gym



Sun, 19 Oct 2003 16:11:01 GMT  
 Check for valid path in class Property Let
Not the Len function per se, but comparisons using integers (either Integer
or Long data type) are faster than string comparisons. In this case, the Len
function is just the means to the end.

Mike


Quote:
> Hi Michael

> > Or better:-
> >     If Len(Dir$(strValue, vbDirectory)) > 0 then
> I've read that using Len is faster/better than > "" ... gotta use it more.

> Thanks

> --
> Dave Keighan



Tue, 21 Oct 2003 07:21:25 GMT  
 Check for valid path in class Property Let
Just to throw in another option......

Public Property Let DirectoryPath(strValue As String)

    'Code to validate the directory path

    On Error GoTo EH

    If GetAttr(strValue) And vbDirectory = vbDirectory Then
        strDirectoryPath = strValue
    End If

EH:

End Property

If you wanted to handle specific error numbers in the error handler, you
could.  But, I'm not sure what the point would be since the only thing that
could really cause an error would be the GetAttr function, and if it causes
an error the path is not valid.  This code will work whether there is a
trailing backslash or not.  The only other thing I'd suggest is you'd
probably want the corresponding Let property (or strDirectoryPath) to ALWAYS
have the trailing backslash or NEVER have the trailing backslash....as
opposed to it sometimes being there and sometimes not.  IOW, consistancy.

Mike


Quote:
> Hi there...

> My problem might turn out to be quite simple, but it's got me stumped, I
> want use a certain directory path in one of my objects in order to parse
the
> directory path... but in the property let procedure before setting the
> property, I'd like to validate this directory path... for example...

> Public Property Let DirectoryPath (strValue As String)
>     'Code to validate the directory path?
>     strDirectoryPath  = strValue
> End Property

> Is there a way to validate this directory path from within the class?



Tue, 21 Oct 2003 07:55:12 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. How can I check a valid path?`

2. Property Get, Set Let with Classes

3. Class Property Get Let

4. Syntax - Property Let for class object

5. Property Let/Get in Classes

6. Property Let/Get in Classes.

7. vbs Class: Property Let Problem

8. newbie--class property let and get

9. class string properties in let statements and nulls

10. property get / property let

11. Property Let, Property Get

12. Property Let vs. Property Set

 

 
Powered by phpBB® Forum Software