+ Reply to Thread
Results 1 to 8 of 8

class structures

Hybrid View

  1. #1
    Registered User
    Join Date
    06-21-2007
    Location
    Vancouver, Canada
    Posts
    74

    class structures

    I am not too familiar with working with visual basic class objects (but worked with C++ class objects before). I'm trying to modify another developer's code separately, so I am trying to incorporate a minimal class definition required to help myself with the development. Given the following subroutine:

    Sub ifctStage_class_test()
    
    On Error GoTo localErr          ' Error Handling
    
    Dim filepath As String          ' for file manipulation
    Dim testStages() As ifctStage
    
    Set testStages(0) = New ifctStage
    
    Exit Sub
    
    localErr:       'only operates correctly if user is operating on Angstrom network with smtp server setup
        MsgBox "error occurred"
        Exit Sub
    End Sub
    with the following class declaration:
    'Angstrom Power
    'Data Analysis Tool v0.1
    'Programmer: Bruno Bate, Stanley Lee
    'Created: 08/03/07 [mm/dd/yyyy]
    'Last Modified: 14/11/07
    
    'Overview: class pertaining to each stage of an AFCT test.  An instance of the class is called
    'whenever a new stage is encountered.
    Option Explicit
    
    Private rawStageData(5, 1000000) As Double      'array holding .csv file data
    
    'variables for info stored in headers
    Dim UUT As String               ' Fuel Cell Identifier
    Dim teststation As Integer      ' test station identifier
    Dim mode As String              ' test mode
    Dim setPoint As Double          ' current set-point
    Dim sampleT As Long             ' Sampling period
    Dim startTime As String         ' Start Date and Time
    Dim comment As String           ' comments
    Dim numSamples As Long          ' number of samples
    
    
    'placeholder variables for columns.Referencing these will clean code and simplify reading
    Private xStage As Long          'Stage column placeholder
    Private xTime As Long           'time column placeholder
    Private xVoltage As Long        'voltage column placeholder
    Private xCurrent As Long        'current column placeholder
    Private xT1 As Long             'T1 temperature column placeholder
    Private xT2_Mflow As Long       'T2/Mass Flow colum placeholder
    Private xPower As Long          'power column placeholder
    
    'Subroutine executed when new instance of class created
    Private Sub Class_Initialize()
       'initialize public class variables
       UUT = "blank"
       teststation = 0
       sampleT = 0
       mode = "blank"
       setPoint = 0
       startTime = "blank"
       xStage = 0
       xTime = 1
       xVoltage = 2
       xCurrent = 3
       xT1 = 4
       xT2_Mflow = 5
       xPower = 6
       
    End Sub
    Is there something missing in the class definition in order to have my subroutine working?

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello stanigator,

    A class has to be placed into a VBA Class Module. The name of this module is what VBA will refer to when you instantiate the Class using a Dim statement. So far, your class has only private data. Data that is hidden from the user that is used internally for the Class structure. To allow the user to interact with the Class you must create public properties, by using the Property Get, and Property Set statements along with methods. Methods are public Sub and/or Functions procedures design to manipulate Class information. A Class in VBA is not really the same as in C or C++. VBA Class objects don't inherit other objects properties and methods. Also they only support a single instance of creation.

    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    06-21-2007
    Location
    Vancouver, Canada
    Posts
    74
    For the class, I did indeed put it inside a VBA class module. I have looked up Property Get and Property Set statement descriptions on MSDN, but still quite unsure of how to use them to get my classes working. Do you happen to have an example? Would be much appreciated.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Stanigator,

    I can code something up this evening, and post it for you. I have to attend to some other business shortly, and won't be able to do anything until later this evening.

    Sincerely,
    Leith Ross

  5. #5
    Valued Forum Contributor
    Join Date
    10-15-2007
    Location
    Home
    MS-Off Ver
    Office 2010, W10
    Posts
    373
    Hi stanigator

    To get you started:

    Your error is in the standard module. You are not allocating space to the array.

    Dim testStages() As ifctStage
    declares an array with no space allocated. You have to use ReDim to size the array. For example, in the standard module:

    ...
    Dim testStages() As ifctStage
    
    ReDim testStages(0 To 4)
    
    Set testStages(0) = New ifctStage
    
    MsgBox testStages(0).xtimeP
    
    Exit Sub
    ...
    The ReDim statement allocates space to 5 objects.

    I display the value of the property xtimeP, that reflects the value of the object's private variable xtime. To get it, add at the end of your ifctStage class module:

    Property Get xtimeP() As Long
    xtimeP = xtime
    End Property
    HTH
    lecxe

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello stanigator,

    Here is simple class. Hopefully this will clearly illustrate the basic components, and structure of a class module.

    Add a Class Module to project and rename it StopWatchCls. The class contains 4 properties: EventID, Hours, Minutes, Seconds. EventID is a read/write property, while the others are read only. There are 3 methods: StartWatch, StopWatch, and Elapsed. Only Elapsed returns a value. It returns the elapsed time as string formatted in "HH:MM:SS".

    Class Module Code
    'Stop Watch Class -
    '  Properties: Hours, Minutes, and Seconds
    '  Methods: Start, Stop, and Elapsed
    
    '-----------------------------------------'
    ' Declare Private Variables for Class use '
    '-----------------------------------------'
    
      Dim Hrs As Long
      Dim Mins As Long
      Dim Secs As Long
      Dim StartTime As Double
      Dim StopTime As Double
      Dim ET As Double
      Dim pvtEventID As String
    
    '-------------------------------'
    ' Public Properties - Read Only '
    '-------------------------------'
    
      Public Property Get Hours() As Long
        Hours = Hrs
      End Property
        
      Public Property Get Minutes() As Long
        Minutes = Mins
      End Property
    
      Public Property Get Seconds() As Long
        Seconds = Secs
      End Property
      
    '-------------------------------'
    '  Public Property - Read/Write '
    '-------------------------------'
    
      Public Property Get EventID() As String
        EventID = pvtEventID
      End Property
       
      Public Property Let EventID(Event_ID As String)
        pvtEventID = Event_ID
      End Property
       
    '----------------'
    ' Public Methods '
    '----------------'
    
      Public Sub StartWatch()
        StartTime = Now()
          Hrs = 0
          Mins = 0
          Secs = 0
      End Sub
    
      Public Sub StopWatch()
        StopTime = Now()
      End Sub
      
      Public Function Elapsed() As String
        ET = StopTime - StartTime
        Elapsed = Format(ET, "hh:mm:ss")
          ET = ET / (1 / 86400)     'Elapsed time is stored in seconds
          Hrs = Int(ET / 3600)
          Mins = Int(ET / 60)
          Secs = ET Mod 60
      End Function
    StopWatch Example
    Place this code in a Standard VBA module
    Sub StopWatchTest()
    
      Dim I As Long, J As Long
      Dim H As Long, M As Long, S As Long
      Dim ID As String
      Dim MyStopWatch As New StopWatchCls
      Dim X As String
      
        MyStopWatch.StartWatch
          For I = 1 To 5000
            For J = 1 To 256
              n = n + Val(Cells(I, J))
            Next J
          Next I
        MyStopWatch.StopWatch
        
        With MyStopWatch
          .EventID = "Time Test 1"
          X = .Elapsed
          H = .Hours
          M = .Minutes
          S = .Seconds
          ID = .EventID
        End With
        
    End Sub
    Sincerely,
    Leith Ross
    Last edited by Leith Ross; 11-15-2007 at 01:01 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1