[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

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

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
runnerjp2005
Asked:
runnerjp2005
1 Solution
 
CodeCruiserCommented:
You can create an inner for loop for the items in Scoresplit.
0
 
Paul JacksonSoftware EngineerCommented:
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
 
Ken ButtersCommented:
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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now