Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Operate on a custom VBA Class Object

Status
Not open for further replies.

kevindurette

Mechanical
May 4, 2008
79
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
 
Replies continue below

Recommended for you

You might check out Tek-tips.com - a sister to this site. The computer geeks live over there.
 
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
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor