?
Solved

Code rewrite for IF Else condition in VB.Net

Posted on 2014-09-17
5
Medium Priority
?
330 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 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

770 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