Calculation not working

persol

New member
Joined
May 29, 2008
Messages
4
Programming Experience
Beginner
Hi, I want to call a calculation from a class into my forum. I just keep getting zero. I want
MessageBox.Show(FormatNumber(school.Ratio, 1))
to show calculation of
numstudents / numfaculty
from my class. here is my code. What am I doing wrong?

form1.
VB.NET:
Public Class college
    Dim name As String
    Dim numstudents As Integer
    Dim numfaculty As Integer
    Dim SFRatio As Double

    Public Property collegeName() As String
        Get
            Return name
        End Get
        Set(ByVal value As String)
            name = value
        End Set
    End Property

    Public Property StudentAmnt() As String
        Get
            Return numstudents
        End Get
        Set(ByVal value As String)
            numstudents = value
        End Set
    End Property

    Public Property facltyNum() As String
        Get
            Return numfaculty
        End Get
        Set(ByVal value As String)
            numfaculty = value
        End Set
    End Property

    Public Property Ratio() As Decimal
        Get
            Return SFRatio = numstudents / numfaculty
        End Get
        Set(ByVal value As Decimal)
            SFRatio = value
        End Set
    End Property


End Class
 
What am I doing wrong?
Just about everything... You have to turn on Option Strict.

SFRatio = numstudents / numfaculty is a Boolean expression, you are comparing value SFRatio with the value of numstudents / numfaculty, then returning True/False implicitly converted to Decimal. If you want to return the result of expression numstudents / numfaculty then just Return numstudents / numfaculty.

What is your intension with using a Set property for this? Properties that return the result of a calculation should be ReadOnly. Since value is a result of combining other property values so there is no logic for user to set this value, not knowing which part of the expression would be changed. In this particular case which of numstudents or numfaculty should implicitly be changed if user changed the ratio? Answer is of course none, if either of these factors is to be changed then they must have their own accessors.

facltyNum and StudentAmnt properties have wrong return type. And they have bad naming. facltyNum and collegeName properties and your class also have wrong name casing.

Even though your Dim'ed fields are Private by default you should set access level explicitly to Private for clarity and readability. IDE will help you with these things, try to write "property" and press Tab key and see what happens. Yes, the whole property structure and private field is generated for you, typically all you have to do is fill in name and type.

Hope your teacher is happy with my answers ;)
 
Back
Top