Solved

Code rewrite for IF Else condition in VB.Net

Posted on 2014-09-17
5
312 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 18

Accepted Solution

by:
Peter Hutchison earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

17 Experts available now in Live!

Get 1:1 Help Now