my mBookSale is not declared But Why ??
-
I have made an instance of the BookSale Object after the mnuCalculate but when I try to access any properties of mBookSale in a a new event sub procedure I get the message mBookSale is not declared.
Private Sub mnuCalcSale_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCalcSale.Click Dim mBookSale As New BookSale mBookSale = New BookSale With mBookSale .Title = txtTitle.Text .Quantity = CInt(txtQuantity.Text) .Price = CDec(txtPrice.Text) End With lblExtendedPrice.Text = FormatNumber(mBookSale.ExtendedPrice) End Sub
the above is all ok, below does not work! BookSale is a Class Module.Private Sub mnuSummary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSummary.Click strMessage = "sales Total " & FormatNumber(mBookSale.SalesTotal) MessageBox.Show(strMessage, "Summary Totals", MessageBoxButtons.OK, MessageBoxIcon.Hand) End Sub
When I try to access mBookSale.SalesTotal I get mBookSale is not declared. the Class module is listed below.Public Class BookSale Private mstrTitle As String Private mintQuantity As Integer Private mdecPrice As Decimal Private Shared mdecSalesTotal As Decimal Private Shared mintSalesCount As Integer #Region "Properties" ' below code to set and return or'GET' Title property 'are properties 'PUBLIC' by default???????? Public Property Title() As String Get Title = mstrTitle End Get Set(ByVal Value As String) mstrTitle = Value End Set End Property Public Property Quantity() As Integer Get Quantity = mintQuantity End Get Set(ByVal Value As Integer) If Value >= 0 Then mintQuantity = Value End If End Set End Property Public Property Price() As Decimal Get Price = mdecPrice End Get Set(ByVal Value As Decimal) If Value >= 0 Then mdecPrice = Value End If End Set End Property Public Function ExtendedPrice() As Decimal Dim decExtendedPrice As Decimal decExtendedPrice = mdecPrice * mintQuantity Return decExtendedPrice mdecSalesTotal += decExtendedPrice
-
I have made an instance of the BookSale Object after the mnuCalculate but when I try to access any properties of mBookSale in a a new event sub procedure I get the message mBookSale is not declared.
Private Sub mnuCalcSale_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCalcSale.Click Dim mBookSale As New BookSale mBookSale = New BookSale With mBookSale .Title = txtTitle.Text .Quantity = CInt(txtQuantity.Text) .Price = CDec(txtPrice.Text) End With lblExtendedPrice.Text = FormatNumber(mBookSale.ExtendedPrice) End Sub
the above is all ok, below does not work! BookSale is a Class Module.Private Sub mnuSummary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSummary.Click strMessage = "sales Total " & FormatNumber(mBookSale.SalesTotal) MessageBox.Show(strMessage, "Summary Totals", MessageBoxButtons.OK, MessageBoxIcon.Hand) End Sub
When I try to access mBookSale.SalesTotal I get mBookSale is not declared. the Class module is listed below.Public Class BookSale Private mstrTitle As String Private mintQuantity As Integer Private mdecPrice As Decimal Private Shared mdecSalesTotal As Decimal Private Shared mintSalesCount As Integer #Region "Properties" ' below code to set and return or'GET' Title property 'are properties 'PUBLIC' by default???????? Public Property Title() As String Get Title = mstrTitle End Get Set(ByVal Value As String) mstrTitle = Value End Set End Property Public Property Quantity() As Integer Get Quantity = mintQuantity End Get Set(ByVal Value As Integer) If Value >= 0 Then mintQuantity = Value End If End Set End Property Public Property Price() As Decimal Get Price = mdecPrice End Get Set(ByVal Value As Decimal) If Value >= 0 Then mdecPrice = Value End If End Set End Property Public Function ExtendedPrice() As Decimal Dim decExtendedPrice As Decimal decExtendedPrice = mdecPrice * mintQuantity Return decExtendedPrice mdecSalesTotal += decExtendedPrice
The reason you get this error is that
mBookSale
is indeed not declared in themnuSummary_Click()
event handler. You have declared it inmnuCalcSale_Click()
but it is only has local scope within that event - it is destroyed whenmnuCalcSale_Click()
exits. If you want themBookSale
object to be available to all events, you'll need to declare it at a higher scope level (i.e. within the class that contains the event handlers)....Steve
1. quod erat demonstrandum 2. "Give a man a fish and you've fed him for a day. Teach him how to fish and you've fed him for life." I read that somewhere once :-) -
The reason you get this error is that
mBookSale
is indeed not declared in themnuSummary_Click()
event handler. You have declared it inmnuCalcSale_Click()
but it is only has local scope within that event - it is destroyed whenmnuCalcSale_Click()
exits. If you want themBookSale
object to be available to all events, you'll need to declare it at a higher scope level (i.e. within the class that contains the event handlers)....Steve
1. quod erat demonstrandum 2. "Give a man a fish and you've fed him for a day. Teach him how to fish and you've fed him for life." I read that somewhere once :-)Dear Steve, Thanks for the reply. I was coming to that conclusion. I was getting confused with scope after an exercise where the objects were created in a frmLoad_ Event(local scope). To populate a ListBox using Item.Add(NewObject(x,y,z) . Then in a new procedure --> ListBox_Click Event <-- the object was addressed via its method ListBox.SelectedItem.GetPrice This is still a bit murky for me....but I think I see that the ListBox contains the actual object so can be directly referenced. Thanks Mick..:zzz: Am I still asleep