Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic user created objects

Status
Not open for further replies.

Aelstro

Technical User
Jun 17, 2003
24
US
I have searched through the forum for anthing related to user defined objects, or user created objects. What I'm looking for is a basic set of code to create an object and attach methods and variables to it.

Something like:

<Define UserObject>

Dim MyObject as new UserObject
MyObject.MyName = ""
MyObject.MyAge = 0
MyObject.ChangeAge(newAge)


MyObject.Myname = Cells(1,1).value

Just the basics of creation, adding properties and methods and using them after they've been created. I'm sure it's out there but I haven't been able to find anything yet.
 
Take a look at the Collection object.
You may also consider a Scripting.Dictionary objet.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thank you. Let me see if I can clarify

I'm not looking to create a group of users, but just any user defined type, or object. Something flexible that I define and set properties and methods to.

It could have been:

Dim MyCar as new Car

where car is my user-defined object having a property called "Doors" so that I can ask for

MyCar.Doors and get the number of doors the car has.

These are quite common in other programming laguages and all of Excel is built on pre-exisiting objects. I just want to create a few of my own and can't see how to do it. Anyone know how?
 
Sounds like you want to make a class module.


Gerry
 
Ahh yes, that sounds about right. how do I go about making a class module and interacting with it once it is created?

 
When in VBE, menu Insert --> Class module

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Alright, now I have a blank screen and the name that excel uses so I can search a bit more

I found this post that gives an example of using a class module


But can anyone give a good solid example of the different possibilities with classes. I can see the "Get" and "Let" uses in the above example, but don't understand them. Maybe point me to a link that explains how to use class modules?

Thanks
 
Ok, this is what I was looking for, here it is if anyone else needs to create class modules (or new user defined types, or user defined objects as they are called by different programming languages)

First Menu Inser--> Class Module

Then you define your Property value:
Dim intDoors as integer

Then make it so Excel can access your property. The name of the property you will use (i.e. car.doors) is the name before the parenthesis:

Property Get Doors() As Integer
Doors = intDoors
End Property

Property Let Doors(MyDoors As Integer)
intDoors = MyDoors
End Property

Add funtions for method calls

Funtion CountDoors(front,rear)
{
' Just like a normal function
}

then you name your class in the properties window

Now you can use your class with the following

Dim ForiegnCar As Car
Set Ferrari = New ForiegnCar
Ferrari.Doors = 2
Ferrari.Doors.Front(2,0)

There, hope that helps sum up class modules in one post. If anything else can be done with classes please let me know.

Aelstro
 
In the immediate window (Ctrl-G) type implements and press the F1 key.
You can do the same thing with property

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top