LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default TypeName of a UDT

As a side note, would situations like this be reasons to avoid UDTs in
favor
of classes all together?


I almost always use Classes rather than Types. Well designed classes almost
always lead to a cleaner and more streamlined design. Types are must faster
than Classes, but unless you are using many objects (in the tens of
thousands), the speed difference comes down to comparing microseconds to
milliseconds. The end user will never notice the difference, and if you are
using tens of thousands of anything, you probably shouldn't be using VBA in
the first place.

The tremendous flexibility of classes makes the performance hit well worth
it in many if not most circumstances.

If you are new to classes, you might find useful
http://www.cpearson.com/Excel/Classes.aspx


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


"Pflugs" wrote in message
...
Thanks very much for the code. I do not have time to test it today, but I
will try it out tomorrow.

As a side note, would situations like this be reasons to avoid UDTs in
favor
of classes all together? They seem to be such easy code to write. But,
I'm
getting to the point in my code writing ability that I feel I need to know
about these things.

Thanks,
Pflugs

"Chip Pearson" wrote:

Instead of a Type, use a Class to define your Vector object. For
example,
create a class named CVector with the following
code:

Public X As Double
Public Y As Double
Public Z As Double

Then in your standard code module, define your UDF to accept a
ParamArray
and test how may elements were passed into the UDF. E.g.,

Function VectorLen2(ParamArray Arr() As Variant)

Dim Vect As CVector
Dim X As Double
Dim Y As Double
Dim Z As Double

If UBound(Arr) - LBound(Arr) + 1 = 1 Then
' 1 parameter passed in. Ensure it is a CVector
If IsObject(Arr(LBound(Arr))) = True Then
If StrComp(TypeName(Arr(LBound(Arr))), "cvector", vbTextCompare)
= 0
Then
' objet is a CVector, get coordinates
Set Vect = Arr(LBound(Arr))
X = Vect.X
Y = Vect.Y
Z = Vect.Z
Else
' object is not CVector
Err.Raise 13, "VectorLen2", "Invalid Object Type"
Exit Function
End If
Else
' parameter is not an object
Err.Raise 91, "VectorLen2", "Object Expected"
Exit Function
End If
ElseIf UBound(Arr) - LBound(Arr) + 1 = 3 Then
' 3 parameters passed in, assume coordinates
X = Arr(LBound(Arr))
Y = Arr(LBound(Arr) + 1)
Z = Arr(LBound(Arr) + 2)
Else
' neither 1 nor 3 parameteres passed in. error.
Err.Raise 5, "VectorLen2", "Invalid Parameter"
Exit Function
End If
' compute length of vector
VectorLen2 = ((X ^ 2) + (Y ^ 2) + (Z ^ 2)) ^ 0.5

End Function


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


"Pflugs" wrote in message
...
I created a UDT called VECTOR containing three doubles (i.e., x,y,z). I
also
created a set of UDF's that I want to have inputs of either a range of
3
cells, an array of values (ints, longs, or doubles), or of my UDT
VECTOR.
The problem is that I can't seem to get TypeName(VECTOR_variable) to
return
VECTOR.

That is, I need something to quickly tell me that the input to this
function
is my user defined type VECTOR. I am getting the error message that
says:

"only user-defined types defined in public object modules can be
coerced
to
or from a variant or passed to late-bound functions"

How can I get around this? I don't know anything about class modules,
and
I
really don't want to get that in depth (yet).

Thanks,
Pflugs





 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I know selected picture is a shape but typename says is picture Carlos Lozano Excel Programming 3 July 24th 04 05:44 PM
Rephrasing argument for typename and a different result Hari[_3_] Excel Programming 6 June 15th 04 05:14 PM


All times are GMT +1. The time now is 08:27 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"