if statment...

K

karim

Hello All,
I have this simple if statment, but under the (IF) there is that blue line
for errors. what is wrong with it?


Private Sub Button27_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button27.Click
If
System.Diagnostics.Process.Start("H:\")
Else
MsgBox("the drive is not linked")
End If

End Sub
 
K

kimiraikkonen

Hello All,
I have this simple if statment, but under the (IF) there is that blue line
for errors. what is wrong with it?

Private Sub Button27_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button27.Click
        If
            System.Diagnostics.Process.Start("H:\")
        Else
            MsgBox("the drive is not linked")
        End If

    End Sub

Hi,
Because you're not evaluating any expression and an expression is
truly expected, you must define a condition after "If" followed by
"then" to get rid of error.
Also it would be nice to explain what you're exactly aiming at doing.

Look at:
http://msdn.microsoft.com/en-us/library/752y8abs(VS.80).aspx

'-------------------------------------------------------
If condition [ Then ]
[ statements ]
[ ElseIf elseifcondition [ Then ]
[ elseifstatements ] ]
[ Else
[ elsestatements ] ]
End If

' or

If condition Then [ statements ] [ Else [ elsestatements ] ]
'------------------------------------------------------

Thanks,

Onur Güzel
 
K

kimiraikkonen

hello kimiraikkonen
               thanks for your help. to answer you question for what i'm
aiming at, i'm trying to have a button to open a drive on the network. but i
want it to display a msg if that drive is not mapped or linked to the current
computer.

Hi,
I'm not fully sure about how to determine if a drive is mapped through
an API call, however based on a logic, you may also consider using
DriveInfo object to determine if the specified drive IsReady, then if
it's ready, explore drive content:

'---------------------------------
Private Sub Button27_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button27.Click

' Where letter "Z:" is supposed to be your network drive
Dim mydrive As New IO.DriveInfo("Z:")
If mydrive.IsReady = True Then
' If it's ready, explore drive using Windows Explorer
System.Diagnostics.Process.Start("Z:")
Else
MsgBox("Drive not ready/unavailable")
End If

End Sub
'------------------------------

Hope that makes a bit sense,

Onur Güzel
 
G

Göran Andersson

karim said:
hello kimiraikkonen
thanks for your help. to answer you question for what i'm
aiming at, i'm trying to have a button to open a drive on the network. but i
want it to display a msg if that drive is not mapped or linked to the current
computer.

Use the DriveInfo.GetDrives method to get an array of the drives that
exist on the system. Loop through the array to check if the desired
drive is available or not.
 
J

James Hahn

Try / Catch is the correct way to try to do something and present a message
(or whatever) if it fails. Your code would therefore be:

Try
System.Diagnostics.Process.Start("H:\")
<do whatever processing is needed>
Catch exc as Exception
MsgBox("the drive is not linked") 'Or a message based on the
actual exception
exit sub
End try

But note that a drive letter is not a process that can be started, so
Process.Start is probably not what you need.
 
G

Göran Andersson

James said:
Try / Catch is the correct way to try to do something and present a
message (or whatever) if it fails.

Unless of course you can determine beforehand if the operation would
fail or not without having it to actually fail. In this case this is
possible by checking if the drive is available or not.
 
H

Herfried K. Wagner [MVP]

Göran Andersson said:
Unless of course you can determine beforehand if the operation would fail
or not without having it to actually fail. In this case this is possible
by checking if the drive is available or not.

Well, the availability of a drive can change at any point of time, so
checking if the drive is available beforehand does not replace exception
handling for other statements accessing the drive.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top