Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Code rewrite for IF Else condition in VB.Net

Posted on 2014-09-17
5
Medium Priority
?
339 Views
Last Modified: 2014-09-17
I am writing the below method in a Class layer. I don't want to show the MsgBox.

How to rewrite the code instead of multiple IF END IF Conditions to an elegant manner.


 Dim sftp As New ThirdPartydll.SFtp()
            Dim success As Boolean

            success = sftp.UnlockComponent("Anything for 30-day trial")

            sftp.ConnectTimeoutMs = 5000
            sftp.IdleTimeoutMs = 10000

            success = sftp.Connect(_uri, _port)

            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

            '  Authenticate with the SSH server. Either Password or Public Key Authentication

            success = sftp.AuthenticatePw(_userId, _password)
            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

            '  After authenticating, the SFTP subsystem must be initialized:
            success = sftp.InitializeSftp()
            If (success <> True) Then
                MsgBox(sftp.LastErrorText)
                Exit Function
            End If

Open in new window

0
Comment
Question by:chokka
5 Comments
 
LVL 20

Accepted Solution

by:
Peter Hutchison earned 2000 total points
ID: 40329171
TRy this:

            Dim sftp As New ThirdPartydll.SFtp()
            Dim success As Boolean

            success = sftp.UnlockComponent("Anything for 30-day trial")

            sftp.ConnectTimeoutMs = 5000
            sftp.IdleTimeoutMs = 10000

            If (Not sftp.Connect(_uri, _port)) Then
                Console.writeline(sftp.LastErrorText) ' Display only when debugging
                Exit Function
            End If

            '  Authenticate with the SSH server. Either Password or Public Key Authentication


            If (Not sftp.AuthenticatePw(_userId, _password)) Then
                Console.writeline(sftp.LastErrorText)
                Exit Function
            End If

            '  After authenticating, the SFTP subsystem must be initialized:
            If ( Not sftp.InitializeSftp()) Then
                Console.writeline(sftp.LastErrorText)
                Exit Function
            End If
0
 

Author Comment

by:chokka
ID: 40329189
Thank you, I cant write console.writeline.  There is no diff between console.writeline or msgbox or messagebox.

Code is working perfectly fine.

We will log the error.

I dont want multiple If end if statement. Thougt might be any switch case statement something like that.
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40329311
The way to do it would be with nested ifs

IF condition1 THEN
  somecode
  IF condition2 THEN
    somecode
    IF condition3 THEN
      somecode
    END IF
    somecode
  END IF
  somecode
ENDIF

In this case, condition2 is only checked it condition1 was true

If you don't need to do any code between each IF then you could do it all in one line

IF condition1 ANDALSO condition2 andalso condition3 THEN
  somecode
ENDIF
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 40329575
Although Peter's and ChloesDad's answers are both valid, I would suggest the first one, as it is far more readable than the second one. Also the second one increases the cyclomatic complexity of your code unnecessarily.
0
 

Author Closing Comment

by:chokka
ID: 40329578
Thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question