Results 1 to 16 of 16

Thread: Classes

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Posts
    83

    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 ???

  2. #2
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    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)

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Everything is a class in VB .NET now anyway, so it is kinda of a moot point anyway..
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Posts
    83
    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

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Public Class car
    2.  
    3. Private _colour As String
    4. Private _fuel As Integer
    5. Private _engineRunning As Boolean
    6.  
    7. Public Property Colour() as string
    8.   Get
    9.      Colour=_colour
    10.   End Get
    11.   Set(ByVal Value as string)
    12.      _colour=Value
    13.   End Set
    14. End Property
    15.  
    16. Public Property Fuel() as Integer
    17.   Get
    18.      Fuel=_Fuel
    19.   End Get
    20.   Set(ByVal Value as Integer)
    21.      _Fuel=Value
    22.   End Set
    23. End Property
    24.  
    25. Public Sub startEngine()
    26. 'now this can only be changed via this method which gives you more control
    27.  _engineRunning = True
    28. End Sub
    29.  
    30. Public Sub StopEngine()
    31.   _engineRunning=false
    32. End Sub
    33.  
    34. End Class
    35.  
    36. Public cars(9) As New car()'I wont go into the usefulness of constructors here
    37.  
    38. getFuel = car(carId).fuel
    39.  
    40. car(carId).StartEngine

    And here is a sample of the wonderfulness of inheritence:
    VB Code:
    1. Public Class Van
    2.    Inherits Car
    3.  
    4. Public Cargo as integer
    5. End Class
    6.  
    7. Public vans(9) as new van()
    8.  
    9. vans(vanid).startEngine()'automatically has the methods and properties of the car class
    10.  
    11. 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.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Posts
    83
    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

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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).

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Posts
    83
    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

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. Imports System
    2. Imports System.Windows.Forms
    3. Imports System.Drawing
    4.  
    5. Module Main
    6.  
    7. Sub Main
    8. Dim fMainForm As New MainForm
    9. fMainForm.ShowDialog()
    10. End Sub
    11.  
    12. Public Class MainForm
    13. Inherits System.Windows.Forms.Form
    14.  
    15. 'make an array of car objects
    16. Public cars(9) As New car()
    17.  
    18. Public Sub New
    19. MyBase.New()
    20. Me.WindowState = 2 'Maximize
    21. End Sub
    22.  
    23. Private Sub fMainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    24.  
    25. 'CODE TO SET PROPERTY
    26. cars(3).colour="Red"
    27.  
    28.  
    29. someSub
    30. End Sub
    31.  
    32. Public Sub someSub()
    33.  
    34. 'CODE TO GET PROPERTY
    35. Msgbox(cars(3).colour)
    36. 'or
    37. dim temp as string=cars(3).colour
    38.  
    39. End Sub
    40.  
    41. End Class
    42.  
    43. End Module
    44.  
    45. Public Class car
    46.  
    47. Private _colour As String
    48. Private _fuel As Integer
    49. Private _engineRunning As Boolean
    50.  
    51. Public Property Colour() as string
    52. Get
    53. Colour=_colour
    54. End Get
    55. Set(ByVal Value as string)
    56. _colour=Value
    57. End Set
    58. End Property
    59.  
    60. Public Property Fuel() as Integer
    61. Get
    62. Fuel=_Fuel
    63. End Get
    64. Set(ByVal Value as Integer)
    65. _Fuel=Value
    66. End Set
    67. End Property
    68.  
    69. Public Sub startEngine()
    70. 'now this can only be changed via this method which gives you more control
    71. _engineRunning = True
    72. End Sub
    73.  
    74. Public Sub StopEngine()
    75. _engineRunning=false
    76. End Sub
    77.  
    78. End Class

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Posts
    83
    is the array only visible from within the class. Can it be accessed from other classes and if so, how

  12. #12
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Imports System
    2. Imports System.Windows.Forms
    3. Imports System.Drawing
    4.  
    5. Module Main
    6.  
    7. 'make an array of car objects AVAILABLE TO WHOLE APP
    8. Public cars(9) As New car()
    9.  
    10. Sub Main
    11. Dim fMainForm As New MainForm
    12. fMainForm.ShowDialog()
    13. End Sub
    14.  
    15. End Module
    16.  
    17.  
    18. 'YOU MAY NEED AN IMPORTS TO REFERENCE IT DIRECTLY NOT SURE
    19. Imports Main
    20.  
    21. Public Class MainForm
    22. Inherits System.Windows.Forms.Form
    23.  
    24. Public Sub New
    25. MyBase.New()
    26. Me.WindowState = 2 'Maximize
    27. End Sub
    28.  
    29. 'THE REST OF THE CODE HERE

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Posts
    83
    Thats great. Thanks for all your help.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Posts
    83
    The Public cars(9) As New Car() line doesnt seem to work. It says Arrays cannot be declared with New

  15. #15
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. 'Option 2
    2. dim cars(9) as car
    3.  
    4. 'before you use an element
    5. cars(3)=New car
    6.  
    7. 'Option 3
    8. Dim cars() As car= {New car(), New car(), New car(), New car(), New car(), _
    9.               New car(), New car(), New car(), New car(), New car()}
    10.  
    11. 'then you are all set
    12. MsgBox(cars(2).fuel)

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Posts
    83
    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
  •  



Click Here to Expand Forum to Full Width