|
-
Sep 2nd, 2002, 04:50 PM
#1
Thread Starter
Lively Member
Classes
What are the benefits of using classes rather than using public variables, subs and functions. Whats the key thing that I'm missing that makes them so good ???
-
Sep 2nd, 2002, 06:26 PM
#2
PowerPoster
Re: Classes
Originally posted by nishds2
What are the benefits of using classes rather than using public variables, subs and functions. Whats the key thing that I'm missing that makes them so good ???
pros: Inheritance, methods, events, readonly properties, to name a few
con is that they are slower (at least they were in VB6)
-
Sep 2nd, 2002, 08:21 PM
#3
PowerPoster
You missed one, a big one by my standards: You can reuse them over and over again without having to copy and paste code. You know it works, so you won't need to spend the extra time testing after the first time.
They are a tad bit slower, but not much. VB is now fully OO so the overhead you had with VB6 has been sliced WAY down.
-
Sep 3rd, 2002, 08:49 AM
#4
Everything is a class in VB .NET now anyway, so it is kinda of a moot point anyway..
-
Sep 3rd, 2002, 02:44 PM
#5
Thread Starter
Lively Member
So if I had code something like as follows:
Type car
colour As String
fuel As Integer
engineRunning As Boolean
End Type
Public cars(0 to 9) As car
Function getFuel(carId As Integer) As Integer
getFuel = car(carId).fuel
End Function
Sub startEngine(carId As Integer)
engineRunning = True
End Sub
As I see it this enables me to have 10 different cars and to retrieve and set values from them individually.
Is this the sort of thing that classes are useful for and if so then how do they help
-
Sep 3rd, 2002, 03:16 PM
#6
For one you can associate the methods with the type better with a class. And use properties for more control of their access.
VB Code:
Public Class car
Private _colour As String
Private _fuel As Integer
Private _engineRunning As Boolean
Public Property Colour() as string
Get
Colour=_colour
End Get
Set(ByVal Value as string)
_colour=Value
End Set
End Property
Public Property Fuel() as Integer
Get
Fuel=_Fuel
End Get
Set(ByVal Value as Integer)
_Fuel=Value
End Set
End Property
Public Sub startEngine()
'now this can only be changed via this method which gives you more control
_engineRunning = True
End Sub
Public Sub StopEngine()
_engineRunning=false
End Sub
End Class
Public cars(9) As New car()'I wont go into the usefulness of constructors here
getFuel = car(carId).fuel
car(carId).StartEngine
And here is a sample of the wonderfulness of inheritence:
VB Code:
Public Class Van
Inherits Car
Public Cargo as integer
End Class
Public vans(9) as new van()
vans(vanid).startEngine()'automatically has the methods and properties of the car class
vans(vanid).cargo=5 'plus its own
There is more but really you have to pick what is going to best suit your needs for the project at end, whether it be a class or a structure.
-
Sep 3rd, 2002, 03:58 PM
#7
Thread Starter
Lively Member
Ok thats great, but still a couple of things. Where exactly in my project would I put this. Do I just have a separate .vb file for all my classes and put them all one under the other, cars then vans underneath etc. Does it need to be contained within a module or can it just be on its own in the .vb file.
Also where would the Public cars(9) As New car() code go. And if I want to access / set a property in code do I need a dim... before getFuel = car(carId).fuel & car(carId).StartEngine will work
-
Sep 3rd, 2002, 05:24 PM
#8
Classes can be in any file either in a module file, form file, class file, just put it outside of the Module..End Module, Form...End Form, Class...End Class. Really all files are just vb files.
After you lay down the code for the class the rest works just like you would for a type, structure, or any object in VB6. Except you put the New keyword when declaring them (which you also have to do with structures, which replaced types in .NET).
-
Sep 4th, 2002, 12:24 PM
#9
Thread Starter
Lively Member
So in the following code could you put in the Public cars(9) As New car() where ever it goes and also a line to set a property and a line to get a property where I put the comments.
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Module Main
Sub Main
Dim fMainForm As New MainForm
fMainForm.ShowDialog()
End Sub
Public Class MainForm
Inherits System.Windows.Forms.Form
Public Sub New
MyBase.New()
Me.WindowState = 2 'Maximize
End Sub
Private Sub fMainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'CODE TO SET PROPERTY
someSub
End Sub
Public Sub someSub()
'CODE TO GET PROPERTY
End Sub
End Class
End Module
Public Class car
Private _colour As String
Private _fuel As Integer
Private _engineRunning As Boolean
Public Property Colour() as string
Get
Colour=_colour
End Get
Set(ByVal Value as string)
_colour=Value
End Set
End Property
Public Property Fuel() as Integer
Get
Fuel=_Fuel
End Get
Set(ByVal Value as Integer)
_Fuel=Value
End Set
End Property
Public Sub startEngine()
'now this can only be changed via this method which gives you more control
_engineRunning = True
End Sub
Public Sub StopEngine()
_engineRunning=false
End Sub
End Class
Thanks
-
Sep 4th, 2002, 01:44 PM
#10
VB Code:
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Module Main
Sub Main
Dim fMainForm As New MainForm
fMainForm.ShowDialog()
End Sub
Public Class MainForm
Inherits System.Windows.Forms.Form
'make an array of car objects
Public cars(9) As New car()
Public Sub New
MyBase.New()
Me.WindowState = 2 'Maximize
End Sub
Private Sub fMainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'CODE TO SET PROPERTY
cars(3).colour="Red"
someSub
End Sub
Public Sub someSub()
'CODE TO GET PROPERTY
Msgbox(cars(3).colour)
'or
dim temp as string=cars(3).colour
End Sub
End Class
End Module
Public Class car
Private _colour As String
Private _fuel As Integer
Private _engineRunning As Boolean
Public Property Colour() as string
Get
Colour=_colour
End Get
Set(ByVal Value as string)
_colour=Value
End Set
End Property
Public Property Fuel() as Integer
Get
Fuel=_Fuel
End Get
Set(ByVal Value as Integer)
_Fuel=Value
End Set
End Property
Public Sub startEngine()
'now this can only be changed via this method which gives you more control
_engineRunning = True
End Sub
Public Sub StopEngine()
_engineRunning=false
End Sub
End Class
-
Sep 4th, 2002, 02:07 PM
#11
Thread Starter
Lively Member
is the array only visible from within the class. Can it be accessed from other classes and if so, how
-
Sep 4th, 2002, 02:13 PM
#12
From what class? The way I have it it is only public to the MainForm but you can move that line to the Module if you need it public to the whole project.
VB Code:
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Module Main
'make an array of car objects AVAILABLE TO WHOLE APP
Public cars(9) As New car()
Sub Main
Dim fMainForm As New MainForm
fMainForm.ShowDialog()
End Sub
End Module
'YOU MAY NEED AN IMPORTS TO REFERENCE IT DIRECTLY NOT SURE
Imports Main
Public Class MainForm
Inherits System.Windows.Forms.Form
Public Sub New
MyBase.New()
Me.WindowState = 2 'Maximize
End Sub
'THE REST OF THE CODE HERE
-
Sep 4th, 2002, 02:31 PM
#13
Thread Starter
Lively Member
Thats great. Thanks for all your help.
-
Sep 6th, 2002, 04:03 PM
#14
Thread Starter
Lively Member
The Public cars(9) As New Car() line doesnt seem to work. It says Arrays cannot be declared with New
-
Sep 6th, 2002, 06:08 PM
#15
Sorry forgot about that, well you can either use a collection instead of an array, wait until you use a car the first time and then call its New constructor, or declare all 9 elements in the dim statement.
VB Code:
'Option 2
dim cars(9) as car
'before you use an element
cars(3)=New car
'Option 3
Dim cars() As car= {New car(), New car(), New car(), New car(), New car(), _
New car(), New car(), New car(), New car(), New car()}
'then you are all set
MsgBox(cars(2).fuel)
-
Sep 7th, 2002, 06:28 AM
#16
Thread Starter
Lively Member
Thanks again that works fine
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
|