Search code examples
listvb.netdatatabledapper

variable 'stocks' is used before it has been assigned a value in vb.net


variable 'stocks' is used before it has been assigned a value in vb.net is there anything wrong code?.

I made please guide me

 For i = 0 To _myTable.Rows.Count - 1
                    Dim detail = New ItemTransfersDetail With {
                        .No = Convert.ToInt32(_myTable.Rows(i)(0)),
                        .Invno = BtxtInvoNo.Text,
                        .CodeProduct = _myTable.Rows(i)(1).ToString(),
                        .Barcode = _myTable.Rows(i)(2).ToString(),
                        .Colorcode = _myTable.Rows(i)(3).ToString(),
                        .Size = _myTable.Rows(i)(4).ToString(),
                        .Qty = Convert.ToInt32(_myTable.Rows(i)(5).ToString())
                        }

                    Dim stocks = New Stocks With {
'Error code below
                        .QTY_STOCK = (stocks.QTY_STOCK - detail.Qty),
                        .CodeProduct = _myTable.Rows(i)(1).ToString(),
                        .Barcode = _myTable.Rows(i)(2).ToString(),
                        .Colorcode = _myTable.Rows(i)(3).ToString(),
                        .Size = _myTable.Rows(i)(4).ToString()
                        }
                    itrservicedetail.InsertItemTransfersdetail(detail)
                    sservice.Updatestockminus(stocks)
 Public Class Stocks
        Public Property CodeProduct() As String
        Public Property Barcode() As String
        Public Property Colorcode() As String
        Public Property Size() As String
        Public Property QTY_STOCK() As Integer
    End Class

Solution

  • You are declaring the stocks variable and using that variable in the expression that initialises it:

    Dim stocks = New Stocks With { .QTY_STOCK = (stocks.QTY_STOCK - detail.Qty),
    

    How can stocks.QTY_STOCK possibly have a value there when that's the property you're setting with that code? You haven't bothered to explain what you're actually trying to achieve with that code so we can't tell you what it should be like, but it definitely shouldn't be like that.