×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Operate on a custom VBA Class Object

Operate on a custom VBA Class Object

Operate on a custom VBA Class Object

(OP)
Think of this small example.

CODE

Dim dblA as Double
Dim dblB as Double
Dim dblC as Double

dblA = 2
dblB = 4
dblC = dblA + dblB
MsgBox dblC
'Returns 6
I want to write my own data type class to handle numerical data of a different type, such as with complex numbers or high-precision floating point numbers. Is it possible to write a class that allows you to simply swap out the datatype in the above code (from "Double" to "clsSuperDuperDataType" and have something that works out of the box, working with VBA's built-in arithmetic operators and even with literal declarations? Doing a "find and replace" on existing code is where I'd like to go with this.

Thanks.

Durette

RE: Operate on a custom VBA Class Object

(OP)
Half way there:

http://www.cpearson.com/excel/DefaultMember.aspx

A default member allows me to declare the value, but I still don't see how that would allow me to operate in a non-trivial fashion (such as with matrices or complex numbers).

RE: Operate on a custom VBA Class Object

You might check out Tek-tips.com - a sister to this site.  The computer geeks live over there.

RE: Operate on a custom VBA Class Object

You might want to keep an eye on my blog (address below); I'll be starting a series on classes "real soon now" (income producing work allowing).  I'll be starting off with "BiCell" and "TriCell" classes, to work with 2D and 3D coordinates, complex numbers, or anything else that needs 2 or 3 values to describe them.

But I'm not sure that what you want is possible (if I have understood it correctly).  For instance, if you had a complex number class, using two doubles for input, the only way you could work with that using the built in Excel facilities would be to convert the number to a text string, then use the complex number functions via a worksheetfunction call.  That would, I think, be very slow and inefficient.  The alternative would be to use something like the Alglib library, which includes complex number arithmetic, and should work well with a custom class.

Doug Jenkins
Interactive Design Services
http://newtonexcelbach.wordpress.com/
 

RE: Operate on a custom VBA Class Object

It is not possible to change from Double to Cls... because of the syntax requirements of VBA.  In the VB family of languages (VB1-6, VBA, VB.net, VBScript) all simple types are assigned with let (which is optional) and all object types are assigned with set (which is not optional).  So just doing a global search and replace would not work.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources