Results 1 to 2 of 2

Thread: Help need it

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2007
    Posts
    18

    Help need it

    Dear all,

    I am new in visual basic and i would like some help with the classes in visual basic 6. For demo i made a simple class. The code of the class is the following:
    vb Code:
    1. Option Explicit
    2.  
    3. 'local variable(s) to hold property value(s)
    4. Private mvarstatus As Single 'local copy
    5. Private mvardirection As Long 'local copy
    6. Public Sub change_direction(ByVal new_direction As Integer)
    7. If new_direction > 3 Or new_direction < 0 Then
    8.     Me.direction = InputBox("Error in light status.Enter a number 1 for forward 2 left turn  3 right turn ", "Error in Direction")
    9.     Else
    10.     Me.direction = new_direction
    11.     End If
    12.  
    13. End Sub
    14.  
    15. Public Sub Changer_status(ByVal new_status As Integer)
    16. If new_status > 3 Or new_status < 0 Then
    17.     Me.status = InputBox("Error in light status.Enter a number 1 for forward 2 left turn  3 right turn ", "Error in Direction")
    18.     Else
    19.     Me.status = new_status
    20.     End If
    21. End Sub
    22.  
    23. Public Property Let direction(ByVal vData As Long)
    24. 'used when assigning a value to the property, on the left side of an assignment.
    25. 'Syntax: X.direction = 5
    26.     mvardirection = vData
    27. End Property
    28.  
    29.  
    30. Public Property Get direction() As Long
    31. 'used when retrieving value of a property, on the right side of an assignment.
    32. 'Syntax: Debug.Print X.direction
    33.     direction = mvardirection
    34. End Property
    35.  
    36. Public Property Let status(ByVal vData As Single)
    37. 'used when assigning a value to the property, on the left side of an assignment.
    38. 'Syntax: X.status = 5
    39.     If vData > 3 Or vData < 0 Then
    40.     vData = InputBox("Error in light status.Enter a number 1 for green 2 for orange 3 for red", "Error in status")
    41.     Else
    42.     mvarstatus = vData
    43. End If
    44. End Property
    45.  
    46.  
    47. Public Property Get status() As Single
    48. 'used when retrieving value of a property, on the right side of an assignment.
    49. 'Syntax: Debug.Print X.status
    50.     If status > 3 Or staus < 0 Then
    51.     mvarstatus = InputBox("Error in light status.Enter a number 1 for forward 2 left turn  3 right turn ", "Error in Direction")
    52.     Else
    53.     status = mvarstatus
    54.     End If
    55. End Property
    56.  
    57. Private Sub Class_Initialize()
    58. Me.direction = 0
    59. Me.status = 0
    60. End Sub
    So i would like to make a 100 instances of that traffic_lights. The problem occurs when i try to call the class from my main code. The main code follows
    vb Code:
    1. Private Sub Form_Load()
    2. Dim i As Integer
    3. Dim light As traffic_light
    4. Set light = New traffic_light
    5. Dim lights(100) As traffic_light
    6. Set lights(100) = New traffic_light
    7. light.direction = 1
    8. light.status = 1
    9. lights(1).change_direction (1)
    10.  
    11.  
    12. End Sub
    Could someone to help me find out where i making the mistake?
    Thanks in advance for your time.
    Last edited by Hack; Mar 23rd, 2008 at 02:43 AM. Reason: Added Highlight Tags

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Help need it

    See the comments...
    Code:
    'this line defines an array of 101 unset traffic_light objects
    Dim lights(100) As traffic_light 
    
    'This line activates element 67 as a new class instance 
    Set lights(67) = New traffic_light  
    
    '...so the next line is valid
    lights(67).change_direction (1)
    
    'but this line is not valid
    lights(1).change_direction (1)
    Last edited by Milk; Mar 22nd, 2008 at 07:13 AM.

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