User Defined Object

G

Greg Maxey

I am trying to create and object and then create/define a property of that
object. My homework is to make sense of something like this'

Public MyStupidIdea as SomeObjectType
Sub Test()
Set MyStupidIdea = New SomeObjectType
MyStupidIdea.MyPointlessProperty = Something
MsgBox myStupidIdea.MyPointlessProperty
End Sub

Don't laugh, my Sensei is always deadly serious ;-)

The idea of a user defined object really shivers my timbers. I don't have
much experience with them. The only thing I have used is a Class module
with Property Get and Let statements. So I proceded as follows:

I created a new Class Module and named it myObject
In the class module I used the following code:

Option Explicit
Private mTag As String
Public Property Get Tag() As String
Tag = mTag
End Property
Public Property Let Tag(NewValue As String)
mTag = NewValue
End Property

In the project module I used the following code:

Public frmObject As MyObject
Sub Test()
Set frmObject = New MyObject
frmObject.Tag = "Almost there!!"
MsgBox frmObject.Tag
Set frmObject = Nothing
End Sub

This works. My question is do I have to use a class module to create a user
defined object? Is there some generic object in Word that I could use to
set property values for easy use? Something like:

Dim myObject as GenericObject
Set myObject = New GenericObject
myOject.Name = "Greg"
MsgBox myObject.Name
Set myObject = Nothin

Is there a easier or simpler way rather than the Class module?

Thanks.
 
T

Tony Jollans

Hi Greg,
My question is do I have to use a class module to create a user
defined object?
Yes

Is there some generic object in Word that I could use to
set property values for easy use?

No. A Collection can be used along these lines but is not exactly like this.

I'm still trying to digest the original post which triggered your question.
I'll have another look in the morning (technically later this morning).
 
G

Greg Maxey

Tony,

Thanks.

I have the white mouse back on the wheel and studying to understand the
reply to the OP.
 
C

Cindy M -WordMVP-

Hi Greg,
My question is do I have to use a class module to create a user
defined object? Is there some generic object in Word that I could use to
set property values for easy use?
Yes, you need a class module to create an object.

Close, as far as assigning and re-using properties would be a User Defined
Type (UDT). This is a bit less overhead than creating a class, but not as
"durable" if you're going to be doing fancy / persistent things.

And if you're going to be creating lots of UDT-objects, you'll want to store
them in an array so that you can re-access them at need.

A UDT is sort of between using an array for the whole lot, and using a class
(with a collection).

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
G

Greg

Cindy,

All these things Class modules, Arrays, and now UDT are what I would
call in nautical terms "hazards to navigation" ;-)

Could you show me a very basic example of creating a user defined type
and then assigning and reading a property value to and from that type?

Thanks.
 
G

Greg

Cindy,

I sorted out a simple example:

Option Explicit
Private Type MyUDT
Name As String
Age As Long
BD As Date
End Type
Sub Test()
Dim myData As MyUDT
myData.Name = "Greg"
myData.Age = 47
myData.BD = "31 December 1958"

MsgBox myData.Name & "-" & myData.Age & "-" & myData.BD
End Sub

I am still looking into the bit on "store them in an array." Do quite
know what to do there, but still digging.

Thanks again.
 
J

Jean-Guy Marcil

Greg was telling us:
Greg nous racontait que :
Cindy,

I sorted out a simple example:

Option Explicit
Private Type MyUDT
Name As String
Age As Long
BD As Date
End Type
Sub Test()
Dim myData As MyUDT
myData.Name = "Greg"
myData.Age = 47
myData.BD = "31 December 1958"

MsgBox myData.Name & "-" & myData.Age & "-" & myData.BD
End Sub

I am still looking into the bit on "store them in an array." Do quite
know what to do there, but still digging.

This may be a case of the blind leading the blind, but I think she meant
something like this:

Option Explicit

Private Type EmployeeUDT
Name As String
Title As String
Dept As String
End Type

Sub Test()

Dim EmployeeRoll(4) As EmployeeUDT

EmployeeRoll(0).Name = "Greg"
EmployeeRoll(1).Name = "Bill"
EmployeeRoll(2).Name = "Bob"
EmployeeRoll(3).Name = "Bud"
EmployeeRoll(4).Name = "Jen"
EmployeeRoll(0).Title = "Boss"
EmployeeRoll(1).Title = "Assistant"
EmployeeRoll(2).Title = "Co-Assistant"
EmployeeRoll(3).Title = "Coffee maker"
EmployeeRoll(4).Title = "Big Boss"
EmployeeRoll(0).Dept = "Missile Launching"
EmployeeRoll(1).Dept = "Missile Launching"
EmployeeRoll(2).Dept = "Missile Launching"
EmployeeRoll(3).Dept = "Missile Launching"
EmployeeRoll(4).Dept = "Missile Making"

MsgBox EmployeeRoll(0).Name & "-" & EmployeeRoll(0).Title & "-" &
EmployeeRoll(0).Dept

End Sub


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Greg Maxey

JGM,
EmployeeRoll(4).Name = "Jen" will have to be
EmployeeRoll(4).Name = "Susan"

That is my wife and according to her she holds rank of Captain.

Thanks for clarifying and showing me something new.
 
C

Cindy M -WordMVP-

Hi Greg,
EmployeeRoll(4).Name = "Susan"

Thanks for clarifying and showing me something new.
Jean-Guy almost caught what I meant :) Usually, you'd declare
the array as a global variable, so that the data can be re-used
at any time during the "life" of the project (from any
procedure).

Cindy Meister
 
G

Greg Maxey

Cindy,

Still coming to grips with some of the many VBA terms. By "Global" you mean

Public Type

vice

Private Type

Correct?
 
C

Cindy M -WordMVP-

Hi Greg,
Still coming to grips with some of the many VBA terms. By "Global" you mean
that you declare the variable at the top of a standard module, rather than as
part of a procedure. It could be Public or Private (although generally global
would be Public; depends on whether you want to share it with procedures in
other modules or projects).

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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

Top