Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. my mBookSale is not declared But Why ??

my mBookSale is not declared But Why ??

Scheduled Pinned Locked Moved Visual Basic
salesquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MickYL
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • M MickYL

      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

      S Offline
      S Offline
      Steve Pullan
      wrote on last edited by
      #2

      The reason you get this error is that mBookSale is indeed not declared in the mnuSummary_Click() event handler. You have declared it in mnuCalcSale_Click() but it is only has local scope within that event - it is destroyed when mnuCalcSale_Click() exits. If you want the mBookSale 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 :-)

      M 1 Reply Last reply
      0
      • S Steve Pullan

        The reason you get this error is that mBookSale is indeed not declared in the mnuSummary_Click() event handler. You have declared it in mnuCalcSale_Click() but it is only has local scope within that event - it is destroyed when mnuCalcSale_Click() exits. If you want the mBookSale 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 :-)

        M Offline
        M Offline
        MickYL
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups