English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

the first sub runs when i press a button. the first thing it does is checks if a filepath in a text box exists, using another sub. if the path does not exist i want it to finish withough doing any more work in the first sub. once i can do this i will be doing it for several more text boxes and directories.


Private Sub btnApplyandClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApplyandClose.Click
dir1FileExists() 'if the file does not exist, exit sub
uninitiatehotkey()
initiatehotkey()
ShowNotifyIcon()
End Sub



Private Sub dir1FileExists()
Dim sFileName As String
sFileName = ("textapp1dir.text")
Dim fFile As New FileInfo(sFileName)
If Not fFile.Exists Then
MessageBox.Show("Please check the directory of Application 1")
End If
End Sub

also, how can i pass in the text box name so i do not have to repeat sub2 for each text box and dir path?

2007-01-09 14:59:10 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

3 answers

Subs don't return values. Functions do.


Private Sub btnApplyandClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApplyandClose.Click
If dir1FileExists(textapp1dir.text) 'if the file does not exist, exit sub
uninitiatehotkey()
initiatehotkey()
ShowNotifyIcon()
End If
End Sub

Public Function dir1FileExists(ByVal sPath As String) As Boolean
Dim bOut As Boolean = False
Dim fFile As New FileInfo(sPath)
If fFile.Exists Then
bOut = True
End If
Return bOut
End Function

2007-01-09 15:44:36 · answer #1 · answered by Anonymous · 0 0

Justin is right about a function being superior to a sub in almost all cases.
But to your question: you already have the code you need in your comment.

IF file1.exists = false then
EXIT SUB
END IF

The second part is trickier..I'd like a little more info but say you want to check 10 text boxes for the file exist thing.

I'd create a LOOP and build a string like this:

Dim sFileName as STRING
Dim X as integer

For x = 1 to 10
sFileName = "textapp" & X & ".text"
Dim fFile As New FileInfo(sFileName)
If Not fFile.Exists Then
MessageBox.Show("Please check the directory of Application 1")
End If
NEXT

hope that helps

2007-01-09 15:38:50 · answer #2 · answered by rod 6 · 0 0

You should consider using Functions instead of subs. The nice thing about a function is that you can reference it anywhere that's in scope, and it returns a value. It's like a key on a keychain. Just pull it out when you need what it can give you. For instance, examine the code below:

Sub task1
Some code here
while CheckForBugs(myVariable) = False
some more code here
do
more code
End sub

Function CheckForBugs(anyVariable as variant) as Boolean
some code to check for bugs in the variable
if bugs exist
CheckForBugs = True
else
CheckForBugs=False

End Function

By coding like this, you make a list of typical actions, and you build them yourself as functions. Any time you then need to access that code snippit, you just call the function.

Based on your example above, you could pass sFileName into as a string, and have the function do what it has to do with that sFileName, then return a value back to you if you needed, or perhaps return FALSE back to you if it isn't a filepath that you've passed to it.

Good Luck!

2007-01-09 15:16:15 · answer #3 · answered by Justin E 2 · 0 0

fedest.com, questions and answers