Question

I am trying to use string.format on a url to pass several values into the string. It's probably a simple error but I cannot get the following code to work. It doesn't even build the string. Any ideas?

Thanks!

Public Sub getStockData()
    Dim client As New WebClient()
    Dim url As String
    Dim ticker As String = "MSFT"
    Dim lastPrice As String = "l1"
    Dim volume As String = "v0"
    Dim marketCap As String = "j1"
    Dim divYield As String = "x"
    Dim peRatio As String = "r"
    Dim eps As String = "e"

    url = String.Format("http://finance.yahoo.com/d/quotes.csv?s={0}&f={1}{2}{3}{4}{5}{6}", ticker, lastPrice, marketCap, divYield, peRatio, eps)
    Dim results As String = client.DownloadString(url)
    messagebox.show(results)
End Sub
Was it helpful?

Solution

You have 7 values you want to insert (format items {0} thru {6}) but only supply six of them:

url = String.Format(
    "http://finance.yahoo.com/d/quotes.csv?s={0}&f={1}{2}{3}{4}{5}{6}", 
    ticker, lastPrice, marketCap, divYield, peRatio, eps)

the variable named "volume" is not being used ...

EDIT: Using the official MS term "format items" as pointed out by @SpectralGhost!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top