Results 1 to 9 of 9

Thread: Reference to a non-shared member...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    23

    Reference to a non-shared member...

    So in my class we have an assignment to create a cash register. I got it to work fine originally but the assignment calls for a class called CashRegister(which I have never done before), a property called Balance, and add method and a subtract method. So I tried to do that but now all my text boxes, when I reference them, are giving me the error "reference to a nonshared member..". HOw do i fix this? My code is:
    Code:
    Public Class frmRegister
        Private Sub frmRegister_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If txtBalance.Text = "" Then
                txtBalance.Text = "0"
            End If
        End Sub
        Class CashRegister
            Private Balance As Integer
            Private addAmount As Double
            Private subAmount As Double
            Public Property AddMethod As Double
                Get
                    Return addAmount
                End Get
                Set(ByVal value As Double)
                    Dim x As Integer = Val(txtAmount.Text)
                    Dim y As Integer = Val(txtBalance.Text)
                    addAmount = x + y
                End Set
            End Property
            Public Property SubtractMethod As Double
                Get
                    Return subAmount
                End Get
                Set(ByVal value As Double)
                    Dim x As Integer = Val(txtAmount.Text)
                    Dim y As Integer = Val(txtBalance.Text)
                    subAmount = y - x
                End Set
            End Property
    
            Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
                txtBalance.Text = FormatCurrency(addAmount)
            End Sub
    
            Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
              
                txtBalance.Text = FormatCurrency(subAmount)
                If txtBalance.Text < 0 Then
                    MessageBox.Show("Transaction Cannot Result in Negative Balance!")
                    txtBalance.Text = "0"
                End If
    
            End Sub
    
        End Class
    End Class
    ANy help would be appreciated?

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Reference to a non-shared member...

    there's a few thing wrong there... first your class is inside the form... that just makes it only visible inside the class.
    next, your class references items on the form... it should NEVER do that.
    Lastly, you've got some event handlers inside the class... again... that's not what you want... they shouldbe outside the class in the form.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    23

    Re: Reference to a non-shared member...

    So then how do i get the values from the text boxes if I can't reference them? could you please provide examples of what you mean as I don't totally understand what you're telling me. should the form be inside the class then or two separate entities?

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    23

    Re: Reference to a non-shared member...

    Code:
    Class CashRegister
        Private Balance As Integer
        Private addAmount As Double
        Private subAmount As Double
        Public Property AddMethod As Double
            Get
                Return addAmount
            End Get
            Set(ByVal value As Double)
                Dim x As Integer = value
                Dim y As Integer = value
                addAmount = x + y
            End Set
        End Property
        Public Property SubtractMethod As Double
            Get
                Return subAmount
            End Get
            Set(ByVal value As Double)
                Dim x As Integer = value
                Dim y As Integer = value
                subAmount = y - x
            End Set
        End Property
    End Class
    Public Class frmRegister
        Private Sub frmRegister_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If txtBalance.Text = "" Then
                txtBalance.Text = "0"
            End If
        End Sub
        Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
            txtBalance.Text = FormatCurrency(AddMethod)
        End Sub
    
        Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
    
            txtBalance.Text = FormatCurrency(SubMethod)
            If txtBalance.Text < 0 Then
                MessageBox.Show("Transaction Cannot Result in Negative Balance!")
                txtBalance.Text = "0"
            End If
    
        End Sub
    
    End Class
    So this is closer to the original example I had and I think its closer to what you're saying I should do, but I have no idea. I don't understand how I can do the equations without using the textboxes. So when I try to call back the methods with the equations, how do they know which value gets assigned to which. Also, with the code like this, the methods aren't recognized and I know something is definitely wrong with the way its set up. Could someone please help? Remember, I am a newbie and just recently watched a video on classes. I can easily make this register work without a class but for my assignment I need to class it.

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Reference to a non-shared member...

    So then how do i get the values from the text boxes if I can't reference them?
    How does any class (form, textbox, label, whatever) get its values? That's what it has properties for. You assign values to the properties of an instance of the class from whatever source is appropriate. You don't define (and therefore constrain) that source within the property.

    should the form be inside the class then or two separate entities?
    If you really need to ask this (or at least the first part of it) then I suggest you need to go back and start again. It is, as TG clearly states, possible to define a class within the form class BUT in that case it will not be accessible from anywhere other than within the form should you later add further forms or modules to the program. The normal practice is therefore to add new classes as Public objects outside the form structure.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    23

    Re: Reference to a non-shared member...

    Could you please show me some examples, it doesn't even have to relate to my code. I just don't understand. In the videos they referenced the textboxes and they worked but I can't seem to do that at all

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Reference to a non-shared member...

    vb.net Code:
    1. Class CashRegister
    2.     Private Balance As Integer
    3.     Private addAmount As Double
    4.     Private subAmount As Double
    5.     Public Property AddMethod As Double ' how could AddMethod possibly be a property? A function perhaps?
    6.         Get
    7.             Return addAmount
    8.         End Get
    9.         Set(ByVal value As Double) ' so if I had an instance of CashRegister called CR1 and assigned the value
    10.                                                  ' CR1.AddMethod = 7, CR1.AddMethod would = 14 ???????????????
    11.             Dim x As Integer = value
    12.             Dim y As Integer = value
    13.             addAmount = x + y
    14.         End Set
    15.     End Property
    16.     Public Property SubtractMethod As Double
    17.         Get
    18.             Return subAmount
    19.         End Get
    20.         Set(ByVal value As Double)
    21.             Dim x As Integer = value
    22.             Dim y As Integer = value
    23.             subAmount = y - x          ' and CR1.SubtractMethod = 0 ???????
    24.         End Set
    25.     End Property
    26. End Class
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Reference to a non-shared member...

    The class has some issues. For one thing, those shouldn't be properties, they should be functions. You want to take two arguments and return a single value. A property is used to set a member variable, or return a member variable in a class instance. What you should have for AddMethod is this:

    Code:
    Public Function AddMethod(firstValue as double, secondValue as double) As Double
     return firstValue + secondValue
    End Function
    Except that that isn't right, either. I see you have Balance as a class member. That should be exposed as a ReadOnly property (no Set method at all). That also makes the AddMethod a bit different depending on what it is supposed to do. The function I wrote does what your property was doing, but that doesn't seem right, because what is the purpose of a balance member in a class called CashRetgister if the AddMethod function does nothing but add two numbers together and return the result? It seems more likely that what you want is something like this:

    Code:
    Public ReadOnly Property TheBalance As Double
     Get
      Return Balance
     End Get
    End Property
    
    Public Sub AddMethod(newValue as Double)
     Balance += newValue
    End Sub
    The form would still need to have an instance of the CashRegister class as a member for you to use it. You'd then properly convert the strings from the Textbox and pass them as the argument to the AddMethod.
    My usual boring signature: Nothing

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Reference to a non-shared member...

    Two posts while I was writing that, but what I wrote was kind of similar to what dunfiddlin was getting at.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width