User Defined Class ?

  • Thread starter Thread starter Matt
  • Start date Start date
M

Matt

Ok, so I am trying to come up with my own object. I have figured out
that I can create something similar to a struct (Class with no
functions) by using the Type/End Type keywords. I want to also be able
to have functions associated with this class. So I want something like
the following:

*******************************************
Type abc
i as integer
j as integer

public function totalAll()
totalAll = i + j
end function
End Type
*******************************************

Now, obviously this is an extremely simple version of what I am trying
to do, but I cant even get this to work. It falls apart when I add the
totalAll() function to the Type/End Type.

Also, do I need a constructor (when i added Sub()/End Sub I got an
error).
 
Ken said:
The Type needs to be established within a class module. Something like this
would be needed for your simple example:

'''class module Totaller'''
Option Compare Database
Option Explicit

Private Type abc
i As Integer
j As Integer
End Type

Private x As Integer, y As Integer

Public Function totalAll()

Dim a As abc

a.i = x
a.j = y

totalAll = a.i + a.j

End Function

Public Property Let assignI(i As Integer)

x = i

End Property

Public Property Let assignJ(i As Integer)

y = i

End Property
'''Module ends'''

You can then establish a new instance of the class, assigning values to its
properties, and call its totalAll method like so:

Dim t As New Totaller

t.assignI = 2
t.assignJ = 3

Debug.Print t.totalAll

This is of course a very long winded way of performing such a simple task,
but hopefully illustrates the principles involved.

Ken Sheridan
Stafford, England


Thank you all very much ... This all is exactly is what I am looking
for!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top