Casting variables

Harvey

Well-known Member
Joined
Nov 18, 2004
Messages
953
Simple question, but yet I found no answer.

I have a function that returns an object. The object is of the class clsCompany, but it is returned as Object. I can access all the values and functions in it, but I would like to store it in a clsCompany variable again. How can I do that?
 
Hi Greg,

Sorry for the late reply, somehow I didn't get a notifier mail from your post. I just found some interesting info about the LSet function. I think it might fix the problem.
LSet copies a variable from one user defined datatype to another.
(my translation from the dutch excel help :p )
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
hm, i figured this doenst help much either, since LSet only works on user declared types, and it doesnt even support variant on one of the two sides, so I'm questioning the added value of the function.
Anyway, I have made a workaround for my program allready, but I am still wondering if there is a "normal" solution for this.
thanks for the help so far.
 
Upvote 0
Since I already have the problem solved I haven't looked at it that well, but if I run into the problem again I will give it a try.
Thanks

btw, the way I solved it was by using some kind of "wrapper class" a class that has only one attribute in it, which is my clsTest class. Since I can still use it as an object I can just put in into a new clsTest variable.

Thanks for all the helps guys, and sorry for my slow replies.
 
Upvote 0
Hi Tushar,

Thanks for adding that information. If I may, can I pick your brain for a minute? :p Two main questions:

  • Nice article, but I guess I'm just not imaginative to figure out where that might come in handy. I can only come up with two applications for needing to utilize this:
    • If I had the class module code locked up in an add-in where I didn't want to share the code, but wanted to utilized the class module in a project where the code needed to be open/viewable.
    • If the class module was quite large and the object-borrowing workbook was a template that would be replicated many times and therefore this solution to end up saving significant file storage resources.
    Otherwise I would probably just import the class module into the project. In your experience when have you needed to do what you've described in your article?
  • You are much more experienced with class modules than I. Generally speaking, wouldn't you normally want to put functions that act up the class object in some manner inside the class module itself? Have you had instances where you have coded functions/procedures that impact class properties and placed said code outside the class module? [Again, lack of imagination.] What types of problems might lend themselves to such a solution?
TIA for any knowledge you can share.

Regards,
 
Upvote 0
well, the thing is, if you are using classes it is much easier to pass data to other functions and workbooks. If you would run a function that had to return 10 different values (i.e. strings, integers, dates) you can all store them inside a class instance and return it that way.
so, there is nothing you can do with classes that you can't do without, but with complex code it can make things easier and more synoptic.
 
Upvote 0
Hmmm, interesting. I haven't ever needed to pass variables between procedures located in separate workbooks to the degree you imply. At most I might pass a handful; certainly never enough to warrant using a class module. If you are already utilizing the properties/methods/functions of the class module anyway, ok, I get it. If the sole purpose is to pass complex sets of data, would a user-defined type suffice? Just thinking out loud. I'm late getting month-end reporting done, so back to work for me...
 
Upvote 0
yes you are right, except that vba has the same problem with types as it has with classes, and i'm not sure if it's possible to pass a type to a different workbook the same way as tusharm's done it.
 
Upvote 0
Hi Greg,

In addition to the two instances you identified in (a) where this approach would help, the third, and an overriding concern for me, is code proliferation. Why duplicate code if you can avoid it? One example is the menu management code in every one of my add-ins. It's a class with a stub in the ThisWorkbook module to instantiate the menu manager object. The class is a duplicate -- well, it's supposed to be a duplicate -- of the master version in an not-yet-published "TM Utilities" add-in. The first problem is that even if I am disciplined enough to always change only the master, the changes don't propagate to the other add-ins until I manually replace the class module in each add-in with the new master version. That can take months, if not years since some add-ins are so stable that I haven't updated them in a few years. The other potential problem is if I have an 'emergency' update to to cater to the needs of a particular add-in. In this case, I may forget to update the master. Does that happen often? No. I am both rather disciplined and my code is not *that* bad. {grin} But, it did happen once.

Similarly, I have a lot of common code for different functionalities. Code to show help, code to visit my web site, code to...well, you get the idea. So, it would be great if I could take all that code and stick it in a "TM Add-In Control" add-in.

That said, I haven't fully implemented the common control add-in because in my initial tests I ran into some strange and recurring problem but one I could not replicate at will. Basically, every so often I would get the error -- and I paraphrase -- "Fatal error: The callee has disconnected." So, the whole approach got put on the back burner.

As far as the comment in (b) goes, no, I cannot think of any good design that would allow extensive changes to a class modules variables from outside of the module. In fact, *any* change from outside constitutes, IMO, bad design. Well, with one exception. See my comment on UDTs below.

Take that bad design comment a step further. I have a major gripe with the people who designed the core concept of a class module. By the nature of the design, all properties are 'global' to the code inside the module. That, IMO, results in sloppy code. And, there is no decent simple workaround for the problem. What do I mean by this? Consider a class module
Code:
dim iX as integer
property get X() as integer
    X=iX
    ...
property let X(uX as integer)
    iX=uX
    ...
sub someSub1
   'Here I have unconditional and unconstrained access to iX
    ...
sub someSub2
    'The same applies here.  Effectively iX is a global variable in _
     this class module.

As far as UDTs go, I've stopped using them for some time. I don't recall where and how I ran into various restrictions, but UDTs declared in a particular manner cannot be passed to certain procedures. And, they don't work well with classes. So, whenever I need a UDT, I am much more likely to just use a simplified class -- one that contains nothing more than a bunch of public variables.

One final comment. MS's current implementation simplifies the use of worksheet and workbook events as well as userforms. Unfortunately, it comes at a price that IMO is too high. For some material on events see
Monitoring events
http://www.tushar-mehta.com/excel/vba/vba-XL events.htm
particularly the section 'A seemingly simplified implementation within a worksheet object'

Greg Truby said:
Hi Tushar,

Thanks for adding that information. If I may, can I pick your brain for a minute? :p Two main questions:

  • Nice article, but I guess I'm just not imaginative to figure out where that might come in handy. I can only come up with two applications for needing to utilize this:
    • If I had the class module code locked up in an add-in where I didn't want to share the code, but wanted to utilized the class module in a project where the code needed to be open/viewable.
    • If the class module was quite large and the object-borrowing workbook was a template that would be replicated many times and therefore this solution to end up saving significant file storage resources.
    Otherwise I would probably just import the class module into the project. In your experience when have you needed to do what you've described in your article?
  • You are much more experienced with class modules than I. Generally speaking, wouldn't you normally want to put functions that act up the class object in some manner inside the class module itself? Have you had instances where you have coded functions/procedures that impact class properties and placed said code outside the class module? [Again, lack of imagination.] What types of problems might lend themselves to such a solution?
TIA for any knowledge you can share.

Regards,
 
Upvote 0
Tushar,

Thank you very much for taking the time to write that detailed response. I had not thought about helping ride herd on version/revision/update control. And while it's not "happy news" it is reassuring to hear that even better programmers than I occasionally hit errors that are hard to replicate or just inexplicable. I have a couple that have eluded my best attempts. One is particularly vexing because when I step through the code with breakpoints it always works and never errors out. But when I just "let 'er rip" - kablam, kaboom, kaputt.

But I digress. Just wanted to say "much obliged".

Regards,
 
Upvote 0

Forum statistics

Threads
1,215,516
Messages
6,125,285
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top