Solved

Code rewrite for IF Else condition in VB.Net

Posted on 2014-09-17
5
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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