LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Conversion between ancestor and descendant classes

I've been experimenting a bit with LabView object oriented programming, bug I can't figure out how to convert/cast an object to a descendent or ancestor class.  Specifically, how would I:

 

1. Convert an object to an object of a descendent class.  To carry the examples in the manual a bit further:  Let's say I have a generic "Automobile" object, and later discover that the "Automobile" is really a "Ford Model T".  How do I cast, convert, or otherwise obtain a "Ford Model T" object which retains all of the original "Automobile" object's private data?  (Assuming that my "Ford Model T" class is a descendant of my "Automobile" class.) If I try the "To More Specific Class" function, I get a 1448 error, which I assume is because my "Automobile" object isn't really a "Fort Model T" object and doesn't have all of the private data and methods needed to implement "Ford Model T" object.

 

2. Convert an object to an object of an ancestor class.  Again to expand the examples in the manual, how would I convert my "Ford Model T" object to a generic "Automobile" object, possibly loosing all but the "Automobile" object's private data in the process?  This might be useful, for example, if I wanted to preserve the "Automobile" object's private data from my "Ford Model T" object while converting to a class (say "Obsolete Automobile") which is a descendant of "Automobile" but not an ancestor of "Ford Model T"

 

Thanks, in advance, for your suggestions.

 

Mark Moss

Message 1 of 11
(5,715 Views)
Use the to more generic class and to more specific class nodes in the application control palette.

p.s. You should follow my blog. I'm writing an article series introducing to LV object oriented programming. See expressionflow.com

Tomi
--
Tomi Maila
0 Kudos
Message 2 of 11
(5,713 Views)

But Tom you missed the fact that he is getting errors when casting parent class to child class.

He is using the functions but it isn't letting him as we would expect.

-Norm

Message 3 of 11
(5,665 Views)
Sorry, I missed that information. I don't know what the problem is in this case without seeing the actual code. Mark, would you please post some example code you have written so that we can take a look and troubleshoot the problem.

Tomi
--
Tomi Maila
0 Kudos
Message 4 of 11
(5,661 Views)
See Attached Image for issue, also try out for yourself. easy to replicate
0 Kudos
Message 5 of 11
(5,658 Views)
Ok. In LV object-oriented programming you have runtime type and static type for each class type wire. The runtime type is what actually flows in the wire whereas the static type is what the wire looks like. The runtime type is always the same as the static type or one of its descendent classes.

What you can do with "to more specific" and "to more generic" nodes is to change the static wire type but that will have no effect on the actual runtime type of the object. What you are trying to do in the image is to change the runtime type of an object, which is not possible.

Let's assume you have a parent class and a child class. You can pass child class to any VI expecting parent class as input. Then inside such VI the wire static type is parent type but the actual runtime type in this case would be child type. In such cases you can use "to more specific" node to change the wire type. To test this behaviour up, create any class and cast it to LabVIEW Object with "to more generic" node and then back to what it originally was with "to more specific" node.

--
Tomi Maila
0 Kudos
Message 6 of 11
(5,654 Views)
Please bear in mind I'm a real foreigner to OOP, and never really understood the practical aspects of it.

However, I think you need to create a VI which copies the data you want to a new object of the type you want.  This will actually create a new object, but the end effect will be a transferral of the information you require......

This should be possible at least, right.

Again, I'm really OOP-abstinent so I may be on the wrong track (I'm always willing to learn though 😉 )

Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
0 Kudos
Message 7 of 11
(5,641 Views)

My code is a bit large to post, and I had intended to create a simplified version, but haven't had the time.  I think Tomi M's explaination is probably what's happening.  Attached a bit of LabView pseudo-code which, I hope, demonstrates what I would like to do.

Since LabView can't change the run-time type of a wire, I suppose the solution to my problem would involve creating a VI which would accept an ancestor object of the object to be created and copy the all of the ancestor's private data to the newly created object.  In order to do that, I would need a way to either walk up the class hierarchy copying each class's private data as I go, or somehow access the private data directly.  Any ideas?

0 Kudos
Message 8 of 11
(5,632 Views)
Mark, you must be somehow not thinking in objects and using the whole concept of object-oriented programming an obscure way. I'd like to help you but I don't want to help you to go totally wrong way. I suggest that you instead of trying to ask what is technically wrong, as for suggestion how to architecturally implement what ever you are trying to implement. Then I may be able to help you in your real problem which is not really the problem you asked in this thread 🙂

Tomi
--
Tomi Maila
0 Kudos
Message 9 of 11
(5,632 Views)

Sure (I don't particularly want to go/continue down the wrong path either).

Basically, I have a set of classes which handle file IO for a few file formats we use internally.  The class hierarchy is set-up like this, where "Data File.lvclass" provides access to the data and properties in each data-file in a generic manner, while the "On Disk File Format X" classes handle reading/writing that specific file format to/from disk:

Data File.lvclass

On-Disk File Format 1.lvclass

On-Disk File Format 2.lvclass

On-Disk File Format 2 Version 1.lvclass

On-Disk File Format 2 Version 2.lvclass

On-Disk File Format 3.lvclass

etc.

So far, this has worked very well for reading and writing my data-files.  Regardless of the format of the file, I can use the same member VI's of "Data File.lvclass" to access/modify the data, get/set properties stored in the file, etc.  Where I've run into problems is in attempting to convert the data from one file format to another.  For example, I need to read in a file using the "Load" member VI of "On-Disk File Format 1.lvclass", perform a few operations on the file using the methods defined in "Data File.lvclass", and then write the file to disk using the  "Save" member VI of "On-Disk File Format 2 Version 2.lvclass".

0 Kudos
Message 10 of 11
(5,621 Views)