0

Having issues with my program, I'm able to get all the data to pull into the grid and into the correct columns / rows. However, my LINQ Query is wrong I believe and it's not making the 3rd column divide properly and insert the correct data. My results: https://gyazo.com/0f307a10dff4c015a361708ecd53c0e9, Correct Result: https://gyazo.com/99e9915bf4ee4e4aa3fb7f2144da1f94. What can I change in my code to change that from a 0 to the correct percentage? Appreciate all responses! Also an example from the text file it is pulling from is this "American Express,AXP,NYSE,Consumer finance,90.73,93.04,5.56,1.01"

    Public Class frmDow

Structure stock
    Dim company As String
    Dim symbol As String
    Dim exchange As String
    Dim industry As String
    Dim price2013 As Double
    Dim price2014 As Double
    Dim earningsPerShare As Double
    Dim dividend As Double
End Structure

Private Sub btnDetermine_Click(sender As Object, e As EventArgs) Handles btnDetermine.Click

    Dim inputData() As String = IO.File.ReadAllLines("DOW2014.txt")
    Dim stockData(29) As stock
    Dim line, data() As String
    Dim yield As Double

    For i As Integer = 0 To (inputData.Length - 1)
        line = inputData(i)
        data = line.Split(","c)
        stockData(i).company = data(0)
        stockData(i).symbol = data(1)
        stockData(i).price2014 = data(5)
        stockData(i).dividend = data(7)
    Next

    Dim stockQuery = From stock In stockData
                     Where yield = data(5) / data(7)
                     Order By yield Descending
                     Select stock

    For Each stockName In stockQuery
        dgvResults.DataSource = stockData
    Next

    For Each s As stock In stockData
        dgvResults.Rows.Add(s.company, s.symbol, yield)
    Next

End Sub

End Class
1
  • First things first, why are you using a structure? That stock type (which should be named "Stock" either way) should be a class. I'd suggest changing that and seeing if your issue goes away because some issues can be caused by people not properly understanding the implications of using value types. Commented Nov 22, 2016 at 4:44

1 Answer 1

0

Looking at your code, the issue is not going away with that change. The problem is that you never actually set a value for yield, so it can't be anything but zero. What you need to do is to select a yield value for each item in your LINQ query, e.g.

Dim stockQuery = From stock In stockData
                 Where yield = data(5) / data(7)
                 Order By yield Descending
                 Select New With {.Company = stock.company,
                                  .Symbol = stock.Symbol,
                                  .Yield = stock.price2014 / stock.dividend}

dgvResults.DataSource = stockQuery.ToList()

Note that the DataSource is set once only and you don't add any rows manually.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.