Hi Hanumant,

Yes, similar but simpler.  A user-defined type looks much like a
class/object in VBA usage, but has fewer features ands doesn't require Set
commands to associate a variable with it or the New keyword for creating
instances.  The major uses for user-defined types at least historically,
were for writing structered data to files, as they take a fixed number of
bytes (at least if the elements are of fixed size) and allow for random
record retrieval like a database; and for interacting with the Windows API
functions, many of which pass C structs which are the C-language equivalent.
You can of course use them similarly with your own code for passing related
data around. 

 

A class is a custom object.  It can do much more than store and return
values.  Storing and returning values can be done in it's simplest as fields
of the object (public class-level variables; they are treated just like
properties from the calling procedure).  Values and be stored and retrieved
using Property Let, Get, and Set procedures within the class too, which
create properties and are particularly useful when you need to execute code
or have the properties actually effect something when accessed.  Classes can
also have methods (public Sub and Function procedures) and events.  See
Writing a Property Procedure
<http://msdn.microsoft.com/en-us/library/gg278550.aspx> , Executing code
when setting properties
<http://msdn.microsoft.com/en-us/library/gg278912.aspx> , Calling Property
Procedures <http://msdn.microsoft.com/en-us/library/gg264181.aspx> ,
Programming with Objects: Using Classes (Visual Basic VS2008 - same concept,
some syntax differences)
<http://msdn.microsoft.com/en-us/library/x84ydca5%28v=VS.90%29.aspx> 

 

Here's a simple class example:

 

Create a class module, call it "Self" with the following code:

Public Name As String

Public Address As String

 

In a standard module (or elsewhere) enter the following code:

Function WhoIsSelf() As Self

Set WhoIsSelf = New Self

With WhoIsSelf

    .Name = "Samwise Gamgee"

    .Address = "Hobbiton, The Shire, Arthedain, Eriador, Middle Earth" ' (I
think)

End With

End Function

 

Sub Test_WhoIsSelf()

Dim My As Self

Set My = WhoIsSelf

MsgBox "I Am " & My.Name & " and my address is " & My.Address & ".",
vbInformation

End Sub

 

It uses fields of the Self class to store your values, and has the same
effect as the user-defined type example.

 

Regarding Collections: A Collection is a built-in VBA class that you can use
to create an object that holds an ordered, indexed collection of name/value
pairs, where value can have any data type or even be an object.  Built-in
Excel objects often have collections: Sheets() is a collection containing
all the sheets in a workbooks, for example.  Members of a collection don't
have to all have the same data type.  See the help topic for more:
Collection Object <http://msdn.microsoft.com/en-us/library/gg251465.aspx> .

 

One more built-in object for storing and passing data together is the
Dictionary Object <http://msdn.microsoft.com/en-us/library/gg251825.aspx> ,
which allows you to simply store name/value pairs (unordered)

 

Asa

 

 

From: excel-macros@googlegroups.com [mailto:excel-macros@googlegroups.com]
On Behalf Of hanumant shinde
Sent: Thursday, December 22, 2011 9:15 AM
To: excel-macros@googlegroups.com
Subject: Re: $$Excel-Macros$$ Return More than 1 value from Function

 





Thanks a lot Asa,

 

I think this might be some what smilar to class thing. just a guess

 

 

From: Asa Rossoff <a...@lovetour.info>
To: excel-macros@googlegroups.com 
Sent: Thursday, 22 December 2011 3:04 AM
Subject: RE: $$Excel-Macros$$ Return More than 1 value from Function




I won't tackle classes right now, but here are a couple more methods:

4. User-Defined Data Types

5. Collections

 

Here's a user-defined data type example:

Type ContactInfo

    Name As String

    Address As String

End Type

 

Function WhoAmi() As ContactInfo

With WhoAmi

    .Name = "Samwise Gamgee"

    .Address = "Hobbiton, The Shire, Arthedain, Eriador, Middle Earth" ' (I
think)

End With

End Function

 

Sub Test_WhoAmi()

Dim My As ContactInfo

My = WhoAmI

MsgBox "I Am " & My.Name & " and my address is " & My.Address & ".",
vbInformation

End Sub

 

Asa

 

From: excel-macros@googlegroups.com [mailto:excel-macros@googlegroups.com]
On Behalf Of hanumant shinde
Sent: Wednesday, December 21, 2011 1:08 PM
To: Excel Group
Subject: $$Excel-Macros$$ Return More than 1 value from Function

 

Hi,

 

How can we return more than 1 value from 1 function.

 

1. using array

2. using By Ref for the values.

3. Create a class with the properties you require to return and then return
the object of that class from the function.

 

i understand 1st two methods. can somebody explain the 3rd and how to
achieve this with the proper example.

i have never created class so i dont know ANYTHING about it.

-- 
FORUM RULES (934+ members already BANNED for violation)
 
1) Use concise, accurate thread titles. Poor thread titles, like Please
Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will
not get quick attention or may not be answered.
 
2) Don't post a question in the thread of another member.
 
3) Don't post questions regarding breaking or bypassing any security
measure.
 
4) Acknowledge the responses you receive, good or bad.
 
5) Cross-promotion of, or links to, forums competitive to this forum in
signatures are prohibited. 
 
NOTE : Don't ever post personal or confidential data in a workbook. Forum
owners and members are not responsible for any loss.
 
----------------------------------------------------------------------------
--------------------------
To post to this group, send email to excel-macros@googlegroups.com

-- 
FORUM RULES (934+ members already BANNED for violation)
 
1) Use concise, accurate thread titles. Poor thread titles, like Please
Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will
not get quick attention or may not be answered.
 
2) Don't post a question in the thread of another member.
 
3) Don't post questions regarding breaking or bypassing any security
measure.
 
4) Acknowledge the responses you receive, good or bad.
 
5) Cross-promotion of, or links to, forums competitive to this forum in
signatures are prohibited. 
 
NOTE : Don't ever post personal or confidential data in a workbook. Forum
owners and members are not responsible for any loss.
 
----------------------------------------------------------------------------
--------------------------
To post to this group, send email to excel-macros@googlegroups.com

 

-- 
FORUM RULES (934+ members already BANNED for violation)
 
1) Use concise, accurate thread titles. Poor thread titles, like Please
Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will
not get quick attention or may not be answered.
 
2) Don't post a question in the thread of another member.
 
3) Don't post questions regarding breaking or bypassing any security
measure.
 
4) Acknowledge the responses you receive, good or bad.
 
5) Cross-promotion of, or links to, forums competitive to this forum in
signatures are prohibited. 
 
NOTE : Don't ever post personal or confidential data in a workbook. Forum
owners and members are not responsible for any loss.
 
----------------------------------------------------------------------------
--------------------------
To post to this group, send email to excel-macros@googlegroups.com

-- 
FORUM RULES (934+ members already BANNED for violation)

1) Use concise, accurate thread titles. Poor thread titles, like Please Help, 
Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will not get 
quick attention or may not be answered.

2) Don't post a question in the thread of another member.

3) Don't post questions regarding breaking or bypassing any security measure.

4) Acknowledge the responses you receive, good or bad.

5)  Cross-promotion of, or links to, forums competitive to this forum in 
signatures are prohibited. 

NOTE  : Don't ever post personal or confidential data in a workbook. Forum 
owners and members are not responsible for any loss.

------------------------------------------------------------------------------------------------------
To post to this group, send email to excel-macros@googlegroups.com

Reply via email to