×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Is there equivalent in VB .NET to the VFP'S ADDBS()?

Is there equivalent in VB .NET to the VFP'S ADDBS()?

Is there equivalent in VB .NET to the VFP'S ADDBS()?

(OP)
Colleagues,

The subject line says it.
For those not familiar with VFP:
ADDBS(strPath) checks presence of the backslash at the end of the given path, e.g.

CODE --> VFP

strPath = "C\MyPath"
lsPath = ADDBS(strPath) 'Returns "C:\MyPath\" 
I've no problem to "roll-er-own" function in VB, but need to make sure I'm not re-inventing the wheel...

TIA!

Regards,

Ilya

RE: Is there equivalent in VB .NET to the VFP'S ADDBS()?

You may find some information in here, who knows....?


---- Andy

There is a great need for a sarcasm font.

RE: Is there equivalent in VB .NET to the VFP'S ADDBS()?

Or my version (somewhat more powerful than simply adding a backslash):

CODE

    Private Function ADDBS(strPath As String) As String
        ADDBS = IO.Path.GetFullPath(Trim(IO.Path.Combine(strPath, " ")))
    End Function 

RE: Is there equivalent in VB .NET to the VFP'S ADDBS()?

(OP)
Thank you, Andy, but I cannot use it: Company's policies! sad

So, I did "roll-er-own", here's what I have so far:

CODE --> VB

'===================================================================================================================================
Public Function AddBackSlash(ByVal tsPath As String) As String
'===================================================================================================================================
' Purpose       : Ensures the given path string has backslash at the end.
' Description   : Checks if the passed parameter's data type match those of the Function's argument; if not – throws error message 
'                 and returns unchanged parameter.
'                 Checks if it's an empty string, if it is - returns it As-Is.
'                 Checks if it's only a file name, if it is - returns it As-Is.
'                 Checks the given parameter-string in case it's full path to a file, cuts off the file name if it is.
'                 Checks if the given parameter-string has backslash at the end, adds it if it has not.
' Parameters    : Path as String - mandatory
' Returns       : Path with the backslash at the end, as String.
' Side effects  : None.
' Notes         : 1. Generic, applies with .NET Framework ver. 1.1, .NET Core 1.0, .NET Standard 1.0 and higher.
'                 2. Verbose on errors, silent otherwise.
' Author        : Ilya
' Revisions     : 2020-03-09 by Ilya – started 1st draft.
'===================================================================================================================================
Dim lsPath As String = "", lsRet As String = ""

' Parameter's verification
If VarType(tsPath) <> VariantType.String Then
   MsgBox("Invalid parameter passed: " & Chr(34) & "tsPath" & Chr(34) & " must be of type String", MsgBoxStyle.Critical, "Fatal Error: INVALID PARAMETER")
   Return lsRet
End If

If String.IsNullOrEmpty(tsPath) Then
   Return lsRet
End If

If Path.GetFileName(tsPath) <> "" And Path.GetExtension(tsPath) <> "" Then 'Path + File name? Strip off that latter
   lsPath = tsPath.Replace(Path.GetFileName(tsPath), "")
Else
   lsPath = tsPath
End If

If String.IsNullOrEmpty(lsPath) Then ' Only the file name was passed? Return blank string
   Return lsPath
End If

' Check for the closing backslash
If Strings.Right(lsPath, 1) <> Path.DirectorySeparatorChar Then
   lsRet = lsPath & Path.DirectorySeparatorChar
Else
   lsRet = lsPath
End If

Return lsRet
End Function
'=================================================================================================================================== 

Please review and advise if I've missed anything.

TIA!

RE: Is there equivalent in VB .NET to the VFP'S ADDBS()?

Take a look at the String.EndsWith function.

As to your function:

' Parameter's verification
If VarType(tsPath) <> VariantType.String Then
MsgBox("Invalid parameter passed: " & Chr(34) & "tsPath" & Chr(34) & " must be of type String", MsgBoxStyle.Critical, "Fatal Error: INVALID PARAMETER")
Return lsRet
End If

This part is not necessary. Trying to pass in anything other than a string will produce a design-time error, and your program will not even compile much less run.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!

RE: Is there equivalent in VB .NET to the VFP'S ADDBS()?

>Trying to pass in anything other than a string will produce a design-time error

Not quite true - VB.NET will do type conversion - but your point remains valid, since the conversion will result in a string, so the check never fails

RE: Is there equivalent in VB .NET to the VFP'S ADDBS()?

> Checks if it's only a file name, if it is - returns it As-Is.

Are you sure?.

RE: Is there equivalent in VB .NET to the VFP'S ADDBS()?

And one more question - why do you think you need to do this?

I ask because if you use the Path methods it is fairly simple to build correct paths without worrying about adding in backslashes or whatever


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close