declare the properties

Joined
Aug 6, 2014
Messages
2
Programming Experience
1-3
Hi All,
currently in the process of learning VB.net via distance learning, i am probably amongst heros and legends in the community, so if my question is in the wrong section or i am asking very novice questions i do apologize.

i am currently learning inheritance and creating classes.

i guess my question basically is the following.

  • when creating a class i must declare the properties (if i am incorrect please correct me)
  • using get/set i declare what the values will be.
  • here in lies my question, can i enter code under the set code to process a simple equation or even make a label show and say "hello world" if a criteria is met.
  • at the constructor point, i assume no code can be entered here.

essentially im looking for examples of classes and different variants that can be set up.

after long searches on the web i keep coming back to msdn with a description of classes which i am finding hard to understand.

many thanks for any input or advice.
Dave (ireland)
 
Dave,

You will declare private fields in your class that are exposed via setting up properties using get/set. Both get and set can include code to process the data in whatever way you need to. The constructor can also have code and can be overloaded, just as any other function within the class.

VB.NET:
 Public Class myClass

        Private _currentLength As Single
        Private _initialLength as Single

        Public Sub New(ByVal theInitialLength As Single)
            ' you can perform any operations here you like

              ' save the initial length for reference
            _initialLength = theInitialLength 
            _currentLength = theInitialLength 
        End Sub

        Public Property CurrentLength As Single
            Get
                Return _currentLength 
            End Get
            Set(value As Single)
                _currentLength = value
            End Set
        End Property

        Public ReadOnly Property InitialLength As Single
            Get
                Return _initialLength 
            End Get
        End Property

        Public Function InchesToMillimeters() As Single
            Return _currentLength * 25.4
        End Function
        .
        .
        .

    End Class
 
Hi Dave,
many thanks for your help,

with your help i feel as though i have gotten somewhere with an assignment i am working on, here is the class i created.

VB.NET:
Public Class RallyCar

    Private teamname As String
    Private distance As Single
    Private time As Single


    'team name will be unknown
    Public Property RallyCarteamname() As String
        Set(ByVal Value As String)
            teamname = Value
        End Set
        Get
            Return teamname
        End Get
    End Property

    'distance travelled will be unknown
    Public Property RallyCardistance() As Single 'set distance properties
        Set(ByVal Value As Single)
            distance = Value
        End Set
        Get
            Return distance 'return distance data
        End Get
    End Property

    'time taken will be unknow
    Public Property RallyCartime() As Single
        Set(ByVal Value As Single)
            time = Value
        End Set
        Get
            Return time 'return time data
        End Get
    End Property

    'with the now know distance and time calculate the average speed
    Public Function AverageSpeedMPH() As Single
        Dim avgspd As Single
        avgspd = (distance / time) * 60 ' multiply by 60 to bring to mph
        Return avgspd ' return the average speed
    End Function

 ' the subroutine i created, i though i could simply put my equation here and have the avgspd work here, but the function was the only way around it.
    Public Sub New(ByVal Teamname As String, ByVal Distance As Single, ByVal Time As Single)

        Me.teamname = Teamname
        Me.distance = Distance
        Me.time = Time

    End Sub

End Class

it works with the code i have used in the rest of my project to get the results i require.

I dont wish to have anyone to point out the correct way to do it as i feel i learn nothing from being shown the right way rather than being given tips so i can figure it out myself, so pointers or advice is greatly appreciated.

many thanks
Dave
 
Back
Top