-
May 3rd, 2013, 09:47 AM
#1
Thread Starter
Junior Member
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?
-
May 3rd, 2013, 10:14 AM
#2
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
-
May 3rd, 2013, 02:58 PM
#3
Thread Starter
Junior Member
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?
-
May 3rd, 2013, 03:08 PM
#4
Thread Starter
Junior Member
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.
-
May 3rd, 2013, 03:11 PM
#5
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!
-
May 3rd, 2013, 03:18 PM
#6
Thread Starter
Junior Member
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
-
May 3rd, 2013, 03:18 PM
#7
Re: Reference to a non-shared member...
vb.net Code:
Class CashRegister
Private Balance As Integer
Private addAmount As Double
Private subAmount As Double
Public Property AddMethod As Double ' how could AddMethod possibly be a property? A function perhaps?
Get
Return addAmount
End Get
Set(ByVal value As Double) ' so if I had an instance of CashRegister called CR1 and assigned the value
' CR1.AddMethod = 7, CR1.AddMethod would = 14 ???????????????
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 ' and CR1.SubtractMethod = 0 ???????
End Set
End Property
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!
-
May 3rd, 2013, 03:22 PM
#8
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
-
May 3rd, 2013, 03:23 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|