|
-
May 4th, 2010, 10:34 PM
#1
Thread Starter
Member
Debugging Classes
Hi guys. I'm doing a program that will calculate the new price. Iv been using a class to put my code their. Iv been getting errors in my form code.
Heres the class code
Code:
Public Class Computer
Private _id As String
Private _price As Decimal
Public Property Id() As String
Get
Return _id
End Get
Set(ByVal value As String)
End Set
End Property
Public Property Price() As Decimal
Get
Return _price
End Get
Set(ByVal value As Decimal)
End Set
End Property
Public Sub New()
_id = String.Empty
_price = 0D
End Sub
Public Sub New(ByVal model As String, ByVal cost As Decimal)
Id = model
Price = cost
End Sub
Public Function CalcNewPrice(ByVal discountPercent As Decimal) As Decimal
Return _price - _price * discountPercent / 100
End Function
Here's my form code
Code:
Dim computerPurchased As New Computer
Dim isConverted As Boolean
Dim rate As Decimal
Dim newPrice As Decimal
isConverted = Decimal.TryParse(origTextBox.Text, Computer.Price)
If isConverted Then
Computer.Id = modelTextBox.Text
rate = Convert.ToDecimal(discountListBox.SelectedItem.ToString.TrimEnd("%"c))
newPrice = Computer.CalcNewPrice(rate)
newLabel.Text = newPrice.ToString("C2")
Else
MessageBox.Show("The cost must be numeric.", "Computer Workshop", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
the error is in the Computer.Price , Computer.CalcNewPrice and Computer.ID
it says Reference to a non-shared member requires an object reference.
-
May 4th, 2010, 10:41 PM
#2
Re: Debugging Classes
You're trying to access an instance member via the class itself rather than an instance of the class. Here:
vb.net Code:
Dim computerPurchased As New Computer
you're creating a new instance of the Computer class and assigning it to the 'computerPurchased' variable. To access members of that instance you use the variable, not the class.
-
May 4th, 2010, 11:26 PM
#3
Thread Starter
Member
Re: Debugging Classes
So what should I do? thanks!
-
May 4th, 2010, 11:37 PM
#4
Re: Debugging Classes
Use the 'computerPurchased' variable to access those members rather than the Computer class, as I said.
-
May 5th, 2010, 12:05 PM
#5
Thread Starter
Member
Re: Debugging Classes
Thanks, I actually did that. But for some reason my application won't work as expected.
I couldnt get the outcome of the new price.
Is there something wrong with these?
Code:
newPrice = Computer.CalcNewPrice(rate)
Code:
Public Function CalcNewPrice(ByVal discountPercent As Decimal) As Decimal
Return _price - _price * discountPercent / 100
End Function
I need to calculate the new price
-
May 5th, 2010, 12:11 PM
#6
Re: Debugging Classes
Hey,
I don't think you are getting what John is saying. Try the following:
Code:
Dim computerPurchased As New Computer
Dim isConverted As Boolean
Dim rate As Decimal
Dim newPrice As Decimal
isConverted = Decimal.TryParse(origTextBox.Text, computerPurchased.Price)
If isConverted Then
computerPurchased.Id = modelTextBox.Text
rate = Convert.ToDecimal(discountListBox.SelectedItem.ToString.TrimEnd("%"c))
newPrice = computerPurchased.CalcNewPrice(rate)
newLabel.Text = newPrice.ToString("C2")
Else
MessageBox.Show("The cost must be numeric.", "Computer Workshop", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Gary
-
May 5th, 2010, 10:45 PM
#7
Thread Starter
Member
Re: Debugging Classes
Thanks Gary, I actually did that but it didnt compute for the new price.
It always shows an output of $0.00
-
May 5th, 2010, 10:51 PM
#8
Re: Debugging Classes
 Originally Posted by pisotmo
I actually did that
Nothing you posted implied that, and the code you posted implied the opposite.
 Originally Posted by pisotmo
It always shows an output of $0.00
Then you need to actually debug. Debugging does not consist of simply reading the code looking for errors. It involves running the code and watching it in action, seeing what path execution follows and the values of your variables along the way. Put a break point on the top of the code and then step through it line by line, using F10 and F11, and make sure that execution takes the path you expect and your variables contain the values you expect. As soon as something you don't expect happens, you've found a problem.
-
May 6th, 2010, 01:15 AM
#9
Re: Debugging Classes
Agreed.
You are going to need to step through the code, and figure out exactly what is going on.
Are you familiar with how to debug your application? i.e. setting breakpoints, using Asserts, setting watch variables, etc?
Gary
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
|