Visual basic noob question?

Why is it telling me that 'hstockstore' and 'lstockstore' are being used before it has been assigned a value?

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

'Function for the Query button'

Dim HighPrice As Double = 0

Dim LowPrice As Double = 10000000

Dim qprice, qqoh, qlast As Double

Dim Qstknbr As String

Dim hstockStore, lstockstore As String

Dim hqohStore, hpriceStore, hlastStore As Double

Dim lqohStore, lpriceStore, llastStore As Double

Dim Qsr As IO.StreamReader = IO.File.OpenText("inventory.txt")

Do Until Qsr.Peek = -1

Qstknbr = Qsr.ReadLine

qqoh = CDbl(Qsr.ReadLine)

qprice = CDbl(Qsr.ReadLine)

qlast = CDbl(Qsr.ReadLine)

If qprice > HighPrice Then

HighPrice = qprice

End If

If qprice > HighPrice Then

HighPrice = qprice

hstockStore = Qstknbr

hqohStore = qqoh

hpriceStore = qprice

hlastStore = qlast

End If

If qprice < LowPrice Then

LowPrice = qprice

lstockstore = Qstknbr

lqohStore = qqoh

lpriceStore = qprice

llastStore = qlast

End If

Loop

ListBox1.Visible = True

ListBox1.Items.Add(String.Format("Highest Price: ") & hstockStore & FormatCurrency(HighPrice))

ListBox1.Items.Add(String.Format("Stock #: " & "Quantity on Hand: " & hqohStore & "Last Order: " & hlastStore))

ListBox1.Items.Add(String.Format("Lowest Price: ") & FormatCurrency(LowPrice))

ListBox1.Items.Add(String.Format("Stock #: " & lstockstore & "Quantity on Hand: " & lqohStore & "Last Order: " & llastStore))

Qsr.Close()

End Sub

Update:

This is an assignment with this code corresponding to the Query button, supposed to list high and low price along with all 4 lines of info for each. This is from a .txt file in the debug folder in a line by line format.

2 Answers

Relevance
  • Anonymous
    1 decade ago
    Favorite Answer

    hstockstore

    Your ListBox1 is an unconditional listing but there is no way of knowing if hstockstore has ever had anything assigned to it in the previous code as it only gets something assigned based on a conditional statement.

  • Anonymous
    1 decade ago

    They're both being assigned conditionally. If neither condition is true (the compiler doesn't know what's going to happen at runtime), neither one gets assigned.

    Dim hstockStore As String = vbNullString

    Dim lstockstore As String = vbNullString

Still have questions? Get your answers by asking now.