Solved

Code rewrite for IF Else condition in VB.Net

Posted on 2014-09-17
5
313 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 19

Accepted Solution

by:
Peter Hutchison earned 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now