Results 1 to 14 of 14

Thread: HOW To Make METHOD in a Class?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    25

    HOW To Make METHOD in a Class?

    I want to make an accelerate method that adds 5 to the speed field each time it is called?
    Code:
    Public Class CarClass
        Private yearModel As Integer
        Private make As String
        Private speed As Integer
    
        Public Sub New(ByVal yearM As Integer, ByVal make As String, ByVal speed As Integer)
            yearModel = yearM
            make = make
            speed = 0
        End Sub
        Public Sub SetYearModel(ByVal yearM As Integer)
            yearM = yearModel
        End Sub
        Public Sub SetMake(ByVal make As String)
            make = make
        End Sub
        Public Sub SetSpeed(ByVal speed As Integer)
            speed = speed
        End Sub
        Public Function GetyearModel() As Integer
            Return yearModel
        End Function
        Public Function Getmake() As String
            Return make
        End Function
        Public Function Getspeed() As Integer
            Return speed
        End Function
    
    End Class

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: HOW To Make METHOD in a Class?

    vb Code:
    1. Public Sub Accelerate
    2.     speed += 5
    3. End Sub

  3. #3
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: HOW To Make METHOD in a Class?

    You must declare yearModel, speed and make as properties not as subs and functions

    vb Code:
    1. Public Class CarClass
    2.  
    3.     Public Sub New(ByVal yearM As Integer, ByVal make As String, ByVal speed As Integer)
    4.         mintYearModel = yearM
    5.         mintSpeed = speed
    6.         mstrMake = make
    7.     End Sub
    8.  
    9.     Private mintYearModel As Integer
    10.     Public Property YearModel() As Integer
    11.         Get
    12.             Return mintYearModel
    13.         End Get
    14.         Set(ByVal value As Integer)
    15.             mintYearModel = value
    16.         End Set
    17.     End Property
    18.  
    19.     Private mintSpeed As Integer
    20.     Public Property Speed() As Integer
    21.         Get
    22.             Return mintSpeed
    23.         End Get
    24.         Set(ByVal value As Integer)
    25.             mintSpeed = value
    26.         End Set
    27.     End Property
    28.  
    29.     Private mstrMake As String
    30.     Public Property Make() As String
    31.         Get
    32.             Return mstrMake
    33.         End Get
    34.         Set(ByVal value As String)
    35.             mstrMake = value
    36.         End Set
    37.     End Property
    38.  
    39.     Public Sub Accelerate()
    40.         mintSpeed += 5
    41.     End Sub
    42.  
    43. End Class



  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: HOW To Make METHOD in a Class?

    @4x2y. the OP's code would work, but your properties method is more efficient

  5. #5
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: HOW To Make METHOD in a Class?

    Quote Originally Posted by .paul. View Post
    the OP's code would work,
    Actually, it will not work at all because there are many mistakes, e.g. the speed sub will never change the speed because the procedure speed variable shadows the module level speed
    vb Code:
    1. Public Sub SetSpeed(ByVal speed As Integer)
    2.         speed = speed
    3.     End Sub
    Therefore, i think he must learn more about properties, methods and variable's scoop



  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: HOW To Make METHOD in a Class?

    @Op: generally speaking you should implement things that describe the object as properties and things that take action as methods.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    25

    Re: HOW To Make METHOD in a Class?

    My next task is to create a program that makes a car object and then calls the accelerate method five times after each call the accelerate method gets the cars speed and displays it how do i go about doing this? This is how far i gotten?
    Code:
    Public Class CarClass
        Private yearModel As Integer
        Private make As String
        Private speed As Integer
    
        Public Sub New(ByVal yearM As Integer, ByVal make As String, ByVal speed As Integer)
            yearModel = yearM
            make = make
            speed = 0
        End Sub
    
        Sub New()
            ' TODO: Complete member initialization 
        End Sub
    
        Public Sub SetYearModel(ByVal yearM As Integer)
            yearM = yearModel
        End Sub
        Public Sub SetMake(ByVal make As String)
            make = make
        End Sub
        Public Sub SetSpeed(ByVal speed As Integer)
            speed = 0
        End Sub
        Public Function GetyearModel() As Integer
            Return yearModel
        End Function
        Public Function Getmake() As String
            Return make
        End Function
        Public Function Getspeed() As Integer
            Return speed
        End Function
        Public Sub accelerate()
            speed += 5
        End Sub
        Public Sub brake()
            speed -= 5
        End Sub
    End Class
    Code:
    Module Module2
        Sub Main()
            Dim mycar As CarClass
            Dim index As Integer
            mycar = New CarClass
            For index = 0 To 5
                mycar.accelerate()
                Console.WriteLine("the speed is " & mycar.Getspeed())
            Next
    
        End Sub
    
        End Module

  8. #8
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: HOW To Make METHOD in a Class?

    @QuestionPlease
    Please take my code in post #3 to start with because your class is full of mistakes and will never work as you expect (read my post #5)



  9. #9

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    25

    Re: HOW To Make METHOD in a Class?

    @4x2y
    if I were to use your code as a start My next task is to create a program that makes a car object and then calls the accelerate method five times after each call the accelerate method gets the cars speed and displays it how do i go about doing this? This is how far i gotten?
    Code:
    Module Module1
        Sub Main()
            Dim mycar As CarClass
            Dim index As Integer
            mycar = New CarClass()
            For index = 0 To 4
                Console.WriteLine("the speed is " & mycar.Accelerate())
            Next
    
        End Sub
    
    End Module

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: HOW To Make METHOD in a Class?

    vb Code:
    1. Module Module1
    2.     Sub Main()
    3.         Dim mycar As New CarClass()
    4.          
    5.         For index As Integer = 0 To 4
    6.             mycar.Accelerate
    7.             Console.WriteLine("the speed is " & mycar.speed)
    8.         Next
    9.  
    10.     End Sub
    11.  
    12. End Module

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    25

    Re: HOW To Make METHOD in a Class?

    am guessing with my class the problem is in the speed? because i am told to use subs and functions for this console application? can any one tell me what specifically is wrong with my code??
    Code:
    Public Class CarClass
        Private yearModel As Integer
        Private make As String
        Private speed As Integer
    
        Public Sub New(ByVal yearM As Integer, ByVal make As String, ByVal speed As Integer)
            yearModel = yearM
            make = make
            speed = 0
        End Sub
    
        Sub New()
            ' TODO: Complete member initialization 
        End Sub
    
        Public Sub SetYearModel(ByVal yearM As Integer)
            yearM = yearModel
        End Sub
        Public Sub SetMake(ByVal make As String)
            make = make
        End Sub
        Public Sub SetSpeed(ByVal speed As Integer)
            speed = speed
        End Sub
        Public Function GetyearModel() As Integer
            Return yearModel
        End Function
        Public Function Getmake() As String
            Return make
        End Function
        Public Function Getspeed() As Integer
            Return speed
        End Function
        Public Sub accelerate()
            speed += 5
        End Sub
        Public Sub brake()
            speed -= 5
        End Sub
    End Class

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: HOW To Make METHOD in a Class?

    this is the problem:

    vb Code:
    1. Public Sub SetSpeed(ByVal speed As Integer)
    2.     speed = speed
    3. End Sub

    to fix it:

    vb Code:
    1. Public Sub SetSpeed(ByVal speed As Integer)
    2.     me.speed = speed
    3. End Sub

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: HOW To Make METHOD in a Class?

    there were other problems too. try this:

    vb Code:
    1. Public Class CarClass
    2.     Private yearModel As Integer
    3.     Private make As String
    4.     Private speed As Integer
    5.  
    6.     Public Sub New(ByVal yearM As Integer, ByVal make As String, ByVal speed As Integer)
    7.         Me.yearModel = yearM
    8.         Me.make = make
    9.         Me.speed = 0
    10.     End Sub
    11.  
    12.     Sub New()
    13.     End Sub
    14.  
    15.     Public Sub SetYearModel(ByVal yearM As Integer)
    16.         Me.yearModel = yearM
    17.     End Sub
    18.  
    19.     Public Sub SetMake(ByVal make As String)
    20.         Me.make = make
    21.     End Sub
    22.  
    23.     Public Sub SetSpeed(ByVal speed As Integer)
    24.         Me.speed = speed
    25.     End Sub
    26.  
    27.     Public Function GetyearModel() As Integer
    28.         Return Me.yearModel
    29.     End Function
    30.  
    31.     Public Function Getmake() As String
    32.         Return Me.make
    33.     End Function
    34.  
    35.     Public Function Getspeed() As Integer
    36.         Return Me.speed
    37.     End Function
    38.  
    39.     Public Sub accelerate()
    40.         Me.speed += 5
    41.     End Sub
    42.  
    43.     Public Sub brake()
    44.         Me.speed -= 5
    45.     End Sub
    46.  
    47. End Class

  14. #14
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: HOW To Make METHOD in a Class?

    @QuestionPlease

    .paul.'s latest code is OK, but in sub New you should replace

    vb Code:
    1. Me.speed = 0
    with
    vb Code:
    1. Me.speed = speed



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