TypeName of a UDT


G

Guest

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
 
Ad

Advertisements

G

Guest

Disregard my previous - I misinterpreted the question.



If you want to have a variant varable as dummy argument to a function and be
able to pass that variant variable a range, an array or your UDT, I am not
sure how you would do that.
 
G

Guest

By a "general module," do you mean NOT a class module? If so, then yes, I
did declare the type in a general module. The code is below:

Option Explicit

Public Type vector
' Note: 'd' indicates a DOUBLE data type, not a derivative
dX As Double
dY As Double
dZ As Double
End Type

Am I missing some module level declaration?

Thanks,
Pflugs
 
G

Guest

I believe what I want to do is send a UDT (vector) to a variant argument of a
UDF. You can do this with standard data types; why can't I do that with user
defined types?

Thanks,
Pflugs
 
G

Guest

Sounds like we agree on what you want to do.

why can't I do that with user defined types?

I would suspect because the authors of visual basic did not implement code
to support it.

In my experience,
UDF is usually used to indicate/distinguish a function that will be used in
a worksheet. Function is used to describe a procedure that is able to return
a value. in that context, UDF's would be a special subset of functions.
 
Ad

Advertisements

G

Guest

Sorry about the repeat in my reply. Guess I got confused, too.

Well, that's too bad. Still the wording of that error message seems to make
it seem like there is hope.

Instead, I am pretty sure I can send an array to a variant argument, so I
will do that instead. Thanks for the input.

Pflugs
 
C

Chip Pearson

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)
 
G

Guest

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
 
Ad

Advertisements

C

Chip Pearson

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)
 

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