VB.NET

Moderators: seancampbell
Number of threads: 4020
Number of posts: 10026

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Making a summary of values? Posted by tastybrownies on 7 Jun 2008 at 12:40 PM
Well, I have a thing for school due Monday but I'm having a little trouble with it. The project or solution consists of a form and a class. My professor wanted us to transfer all the math to the class and leave the other code in the form, needless to say it's easier said than done.

I am not very experienced with using classes yet and want to accumulate totals that the user has ended up with. Do the variables in the class have to be declared differently than in the form?

Here is what I have so far, it would be great if I could get some much needed help with this.

Right now only the sumTotalComm works correctly...

Public Class SalesPerson
    Const QUOTA_Decimal As Decimal = 1000D
    Const COMMISSION_RATE_Decimal As Decimal = 0.15D
    Const BASE_PAY_Decimal As Decimal = 250D
    Private salesString As String
    Private weeklySalesLessDecimal As Decimal
    Private Shared weeklySalesDecimal, sumTotalCommDecimal, _
    totalSalesDecimal, commDecimal, sumTotalPayDecimal, totalPayDecimal As Decimal
    Private weeklyDecimal As Decimal
    Private Shared sumTotalWeeklyDecimal As Decimal





    'Parameterized constructor.
    Sub New(ByVal Sales As String, ByVal Weekly As Decimal)
        'Assign the property values.

        With Me
            .Sales = Sales
            .Weekly = Weekly
            CalculateComm()
            CalculateWeeklySalesLess()
            CalculateWeekly()
            CalculateSumTotalComm()
            CalculateSumTotalWeeklyDecimal()
        End With
    End Sub

    Property Sales() As String
        Get
            Return salesString
        End Get
        Set(ByVal value As String)
            salesString = value
        End Set
    End Property

    Property TotalWeekly() As Decimal
        Get
            Return sumTotalWeeklyDecimal

        End Get
        Set(ByVal value As Decimal)
            CalculateSumTotalWeeklyDecimal()


        End Set
    End Property
    Property SumTotalComm() As Decimal
        Get
            Return sumTotalCommDecimal
        End Get
        Set(ByVal value As Decimal)
            CalculateSumTotalComm()

        End Set
    End Property

    Property Weekly() As Decimal
        Get
            Return weeklyDecimal
        End Get
        Set(ByVal value As Decimal)
            If value >= 0 Then
                weeklyDecimal = value
            End If

        End Set
    End Property

    Property WeeklySalesLess() As Decimal
        Get
            Return weeklySalesLessDecimal
        End Get
        Set(ByVal value As Decimal)
            If weeklySalesDecimal < 1000 Then
                CalculateWeeklySalesLess()
            End If

        End Set
    End Property

    Property Comm() As Decimal
        Get
            Return commDecimal
        End Get
        Set(ByVal value As Decimal)
            commDecimal = value
        End Set
    End Property

    Protected Overridable Sub CalculateComm()
        'please calculate commission.
        commDecimal = weeklyDecimal * COMMISSION_RATE_Decimal
    End Sub
    Protected Overridable Sub CalculateWeeklySalesLess()
        weeklySalesLessDecimal = BASE_PAY_Decimal
    End Sub

    Protected Overridable Sub CalculateWeekly()
        weeklyDecimal = (weeklyDecimal * COMMISSION_RATE_Decimal) + 250

    End Sub

    Protected Overridable Sub CalculateSumTotalWeeklyDecimal()
        sumTotalWeeklyDecimal = weeklyDecimal
    End Sub

    Protected Overridable Sub CalculateSumTotalComm()
        sumTotalCommDecimal += commDecimal
    End Sub
End Class



Report
Re: Making a summary of values? Posted by seancampbell on 9 Jun 2008 at 8:14 AM
I don't know what your project was, or what the math was, but using class objects is actually a pretty easy concept. In VB.Net, all objects you declare are class objects

So:
Dim strName as String 'strName is an class object type String
'Now String and other similar datatypes are special, but the concepts apply

Class clsTeach

  'Typically this area is used to declare variables
  'Private var types are variables that can only be used by the functions,
  'Sub routines, and properties inside the class object
  Private _Cool as Boolean = True
  'If you just do a Dim, it defaults to Private
  Dim _Name as String = "firesickle.com"

  'Public var types are visible and useable outside the class object
  Public ShowPub as String = "Test"
  
  'Properties are awesome ways to define vartypes.
  'In the beginning it seems pretty abstract, but properties allow you to massage data that is coming in and out
  'There are a great deal of applications here, I will show one here
  Public Property Name as String
  Get
    Return _Name
  End Get
  Set(Value as String)
    _Name = Value
    'Massaging the _Cool variable based on the input to the Name property
    If Value.tolower = "firesickle.com" then
      _Cool = True
    else
      _Cool = False
    End If
  End Set
  End Property

  'ReadOnly properties are used to safely expose private variables
  Public ReadOnly Property Cool as Boolean
  Get
    Return _Cool
  End get
  End Property

  Sub New()

  End Sub

'An override for the Constructor
'This way I can declare a new instance of the Class object
'without passing data
  Sub New(F as String, SP as String)
    _Face = F
    ShowPub = SP
    'If you checked _Cool right now, the value would be True
    'Regardless of what you entered for F
    'This is because I set it using the _Face reference
    'Instead of setting the Face property
    'Typically I define the variables with an underscore
    'and the properties without, so I know which property goes to
    'which variable, but everyone's style is different
    Face = F
    'If you check _Cool here it will have changed if F didn't equal
    'firesickle.com
  End Sub

  Public Sub setCool(C as Boolean)
    'You can set private vars with a Subroutine
    _Cool = C
  End Sub

  Public Function getCool() as Boolean
    'You can get private vars with a Function
    Return _Cool
  End Function

  'But why would you want to do this, when you could have just made
  'a property (which we did, but readonly so I don't confuse you
  'just wanted to show an alternative to exposing private
  'vars with code
End Class

'This code would go under an event on a form
Dim T as clsTeach

'If I try to access anything in T it will crash
'with a null reference error, this is because class objects
'need to be initialized

T = new clsTeach()

'I didn't pass any vars so the New() code got called, which was empty
'If I didn't make an empty Sub New() it wouldn't let me pass nothing

msgBox(T.Face & " - " T.Cool)
T.Face = "SeanC"
msgBox(T.Face & " - " T.Cool)
'Notice you cannot see T._Face and T._Cool

'Play around with this and see what kinds of things you can and can't do.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.