Solved

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

Posted on 2014-01-31
3
348 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

24 Experts available now in Live!

Get 1:1 Help Now