Results 1 to 9 of 9

Thread: Debugging Classes

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    34

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    34

    Re: Debugging Classes

    So what should I do? thanks!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Debugging Classes

    Use the 'computerPurchased' variable to access those members rather than the Computer class, as I said.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    34

    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

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    34

    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

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Debugging Classes

    Quote Originally Posted by pisotmo View Post
    I actually did that
    Nothing you posted implied that, and the code you posted implied the opposite.
    Quote Originally Posted by pisotmo View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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
  •  



Click Here to Expand Forum to Full Width