Solved

Clean up multiple IF statements - advice on how to make it neater!

Posted on 2014-01-31
3
351 Views
Last Modified: 2014-02-02
Hi,

The code below works great but before i move on i have noted it looks alittle messy.

Does anyone have any advice on how i could tidy this up atall to me it legible.

		Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
		
		
	
Dim webGet As New HtmlWeb() 'open the system
        Dim htmlDoc As HtmlDocument = webGet.Load("http://www.livefutbol24.com/Football/Live")'' get the html from the webpage

      

		For Each div As Object In htmlDoc.DocumentNode.SelectNodes(".//div[@class='matches']")' select all the divs within the code that contain *
 
   If Not Div.selectSingleNode(".//td[@class='score']") Is Nothing Then
   
   Dim Scoresplit() As String = Div.selectSingleNode(".//td[@class='score']").InnerText.Split("-") 'split the string for scores

   
   lblHTMLOutput.Text += StringOutNumbers(Div.selectSingleNode(".//td[@class='home']").InnerText)
   if StringOuttext(Div.selectSingleNode(".//td[@class='home']").InnerText) <> "0" then
   lblHTMLOutput.Text += StringOuttext(Div.selectSingleNode(".//td[@class='home']").InnerText)
   end if
   
   
		  if Scoresplit(0).Trim() <> "0" then
			
			lblHTMLOutput.Text += "<font size='3' color='green'>" & Scoresplit(0).Trim() & "</font>"
			
		else
			
			lblHTMLOutput.Text +="<font size='3' color='red'>" & Scoresplit(0).Trim() & "</font>"
			
		end if
		lblHTMLOutput.Text += " - "
		
			 if Scoresplit(1).Trim() <> "0" then
			
			lblHTMLOutput.Text += "<font size='3' color='green'>" & Scoresplit(1).Trim() & "</font>"
			
		else
			
			lblHTMLOutput.Text +="<font size='3' color='red'>" & Scoresplit(1).Trim() & "</font>"
			
		end if
		
				  
		  lblHTMLOutput.Text += StringOutNumbers(Div.selectSingleNode(".//td[@class='away']").InnerText)
		  if StringOuttext(Div.selectSingleNode(".//td[@class='away']").InnerText) <> "0" then
		  lblHTMLOutput.Text += StringOuttext(Div.selectSingleNode(".//td[@class='away']").InnerText)
		  end if
		  
		  if Scoresplit(0).Trim() or Scoresplit(1).Trim() <> "0" then
			
			lblHTMLOutput.Text += "<font size='3' color='orange'>Boom!</font> <br>"
		ELSE
lblHTMLOutput.Text += "<br>"
		end if 
		  
		  end if

        Next

Open in new window

0
Comment
Question by:runnerjp2005
3 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39823836
You can create an inner for loop for the items in Scoresplit.
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 39823844
It doesn't need much just some proper formatting:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        'open the system
        Dim webGet As New HtmlWeb()
        'get the html from the webpage
        Dim htmlDoc As HtmlDocument = webGet.Load("http://www.livefutbol24.com/Football/Live")

        ' select all the divs within the code that contain *
        For Each div As Object In htmlDoc.DocumentNode.SelectNodes(".//div[@class='matches']")
            If Not div.selectSingleNode(".//td[@class='score']") Is Nothing Then

                'split the string for scores
                Dim Scoresplit() As String = div.selectSingleNode(".//td[@class='score']").InnerText.Split("-")

                lblHTMLOutput.Text += StringOutNumbers(div.selectSingleNode(".//td[@class='home']").InnerText)
                If StringOuttext(div.selectSingleNode(".//td[@class='home']").InnerText) <> "0" Then
                    lblHTMLOutput.Text += StringOuttext(div.selectSingleNode(".//td[@class='home']").InnerText)
                End If

                If Scoresplit(0).Trim() <> "0" Then
                    lblHTMLOutput.Text += "<font size='3' color='green'>" & Scoresplit(0).Trim() & "</font>"
                Else
                    lblHTMLOutput.Text += "<font size='3' color='red'>" & Scoresplit(0).Trim() & "</font>"
                End If
                lblHTMLOutput.Text += " - "

                If Scoresplit(1).Trim() <> "0" Then
                    lblHTMLOutput.Text += "<font size='3' color='green'>" & Scoresplit(1).Trim() & "</font>"
                Else
                    lblHTMLOutput.Text += "<font size='3' color='red'>" & Scoresplit(1).Trim() & "</font>"
                End If

                lblHTMLOutput.Text += StringOutNumbers(div.selectSingleNode(".//td[@class='away']").InnerText)
                If StringOuttext(div.selectSingleNode(".//td[@class='away']").InnerText) <> "0" Then
                    lblHTMLOutput.Text += StringOuttext(div.selectSingleNode(".//td[@class='away']").InnerText)
                End If

                If Scoresplit(0).Trim() Or Scoresplit(1).Trim() <> "0" Then
                    lblHTMLOutput.Text += "<font size='3' color='orange'>Boom!</font> <br>"
                Else
                    lblHTMLOutput.Text += "<br>"
                End If
            End If
        Next
    End Sub

Open in new window

0
 
LVL 19

Accepted Solution

by:
Ken Butters earned 500 total points
ID: 39825498
In addition to formatting... I like to identify things that happen more than once and consolidate those into a subroutine or function.

It makes the code a little easier to read, and if you have an error in that function, you only have to fix it once, not everywhere it may have occurred.

An example of this is the following code:

If Scoresplit(1).Trim() <> "0" Then
  lblHTMLOutput.Text += "<font size='3' color='green'>" & Scoresplit(1).Trim() & "</font>"
Else
  lblHTMLOutput.Text += "<font size='3' color='red'>" & Scoresplit(1).Trim() & "</font>"
End If

Open in new window


In this example you do the same thing, once for Scoresplit(0) and again for scoresplit(1).  The purpose of the if statement is just to give a different color.

the following would break it into a function call and simplify your main routine logic:
(and it would eliminate two if-then-else structures)
  Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  Dim webGet As New HtmlWeb() 'open the system
  Dim htmlDoc As HtmlDocument = webGet.Load("http://www.livefutbol24.com/Football/Live")'' get the html from the webpage
  
  For Each div As Object In htmlDoc.DocumentNode.SelectNodes(".//div[@class='matches']")' select all the divs within the code that contain *
    
    If Not Div.selectSingleNode(".//td[@class='score']") Is Nothing Then
      
      Dim Scoresplit() As String = Div.selectSingleNode(".//td[@class='score']").InnerText.Split("-") 'split the string for scores

      lblHTMLOutput.Text += StringOutNumbers(Div.selectSingleNode(".//td[@class='home']").InnerText)

      if StringOuttext(Div.selectSingleNode(".//td[@class='home']").InnerText) <> "0" then
        lblHTMLOutput.Text += StringOuttext(Div.selectSingleNode(".//td[@class='home']").InnerText)
      end if
      
      lblHTMLOutput.Text += "<font size='3' color='" & getColor(Scoresplit(0)) & "'>" & Scoresplit(1).Trim() & "</font>"
    	lblHTMLOutput.Text += " - "
      lblHTMLOutput.Text += "<font size='3' color='" & getColor(Scoresplit(1)) & "'>" & Scoresplit(1).Trim() & "</font>"
      
      lblHTMLOutput.Text += StringOutNumbers(Div.selectSingleNode(".//td[@class='away']").InnerText)
    	
      if StringOuttext(Div.selectSingleNode(".//td[@class='away']").InnerText) <> "0" then
        lblHTMLOutput.Text += StringOuttext(Div.selectSingleNode(".//td[@class='away']").InnerText)
    	end if
    	  
      if Scoresplit(0).Trim() or Scoresplit(1).Trim() <> "0" then
        lblHTMLOutput.Text += "<font size='3' color='orange'>Boom!</font> <br>"
    	ELSE
        lblHTMLOutput.Text += "<br>"
    	end if 

    end if

  Next

Open in new window


here is the new function that is called from the routine above:

Function getColor(Score as string) as string

    if score.trim() <> "0" then 
      getColor = "green"
    else 
      getcolor = "red"  
    end if
    
  End Function

Open in new window

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

Title # Comments Views Activity
Export import database 4 40
ASP.NET 5 Templates 2 65
Reset asp.net login password 4 26
VB.Net - Windows UserName index and length error 4 20
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

14 Experts available now in Live!

Get 1:1 Help Now