|
-
Apr 29th, 2012, 02:33 PM
#1
Thread Starter
Junior Member
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
-
Apr 29th, 2012, 03:01 PM
#2
Re: HOW To Make METHOD in a Class?
vb Code:
Public Sub Accelerate
speed += 5
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 29th, 2012, 03:27 PM
#3
Re: HOW To Make METHOD in a Class?
You must declare yearModel, speed and make as properties not as subs and functions
vb Code:
Public Class CarClass Public Sub New(ByVal yearM As Integer, ByVal make As String, ByVal speed As Integer) mintYearModel = yearM mintSpeed = speed mstrMake = make End Sub Private mintYearModel As Integer Public Property YearModel() As Integer Get Return mintYearModel End Get Set(ByVal value As Integer) mintYearModel = value End Set End Property Private mintSpeed As Integer Public Property Speed() As Integer Get Return mintSpeed End Get Set(ByVal value As Integer) mintSpeed = value End Set End Property Private mstrMake As String Public Property Make() As String Get Return mstrMake End Get Set(ByVal value As String) mstrMake = value End Set End Property Public Sub Accelerate() mintSpeed += 5 End Sub End Class
-
Apr 29th, 2012, 03:38 PM
#4
Re: HOW To Make METHOD in a Class?
@4x2y. the OP's code would work, but your properties method is more efficient
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 29th, 2012, 03:56 PM
#5
Re: HOW To Make METHOD in a Class?
 Originally Posted by .paul.
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:
Public Sub SetSpeed(ByVal speed As Integer) speed = speed End Sub
Therefore, i think he must learn more about properties, methods and variable's scoop
-
Apr 29th, 2012, 04:13 PM
#6
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.
-
Apr 29th, 2012, 04:31 PM
#7
Thread Starter
Junior Member
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
-
Apr 29th, 2012, 04:47 PM
#8
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)
-
Apr 29th, 2012, 05:57 PM
#9
Thread Starter
Junior Member
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
-
Apr 29th, 2012, 06:01 PM
#10
Re: HOW To Make METHOD in a Class?
vb Code:
Module Module1
Sub Main()
Dim mycar As New CarClass()
For index As Integer = 0 To 4
mycar.Accelerate
Console.WriteLine("the speed is " & mycar.speed)
Next
End Sub
End Module
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 29th, 2012, 08:49 PM
#11
Thread Starter
Junior Member
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
-
Apr 29th, 2012, 11:02 PM
#12
Re: HOW To Make METHOD in a Class?
this is the problem:
vb Code:
Public Sub SetSpeed(ByVal speed As Integer)
speed = speed
End Sub
to fix it:
vb Code:
Public Sub SetSpeed(ByVal speed As Integer)
me.speed = speed
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 29th, 2012, 11:09 PM
#13
Re: HOW To Make METHOD in a Class?
there were other problems too. try this:
vb 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)
Me.yearModel = yearM
Me.make = make
Me.speed = 0
End Sub
Sub New()
End Sub
Public Sub SetYearModel(ByVal yearM As Integer)
Me.yearModel = yearM
End Sub
Public Sub SetMake(ByVal make As String)
Me.make = make
End Sub
Public Sub SetSpeed(ByVal speed As Integer)
Me.speed = speed
End Sub
Public Function GetyearModel() As Integer
Return Me.yearModel
End Function
Public Function Getmake() As String
Return Me.make
End Function
Public Function Getspeed() As Integer
Return Me.speed
End Function
Public Sub accelerate()
Me.speed += 5
End Sub
Public Sub brake()
Me.speed -= 5
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 30th, 2012, 06:48 AM
#14
Re: HOW To Make METHOD in a Class?
@QuestionPlease
.paul.'s latest code is OK, but in sub New you should replace
with
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|