LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Preallocated VI in a class

Solved!
Go to solution

Am I correct in thinking that you cannot use a preallocated reentrant vi in a class and expect a new instance of the VI for each class instance? Any recommendations on how to do this without resorting to restructuring the reentrant VI and storing all of its data in the class instance.

 

My use case is, I have a load of heaters which I was considering creating a class for. One of my methods would be set duty cycle which would use the PID.vi (Preallocated reentrant). If I put this in one of my class VI's (even if that is set to preallocated as well) it appears to share the same instance of PID.vi amongst all of the class instances.

0 Kudos
Message 1 of 5
(999 Views)
Solution
Accepted by topic author Worle

@Worle wrote:

Am I correct in thinking that you cannot use a preallocated reentrant vi in a class and expect a new instance of the VI for each class instance? 


No, a preallocated reentrant VI will get a new instance everywhere you (statically, sigh) use it.

 

So basically, everywhere you use it is a new instance.

 

The class instance has little to do with it. A reentrant VI looping over an array of objects will keep it's data over the iterations. As you've noticed  yourself.

 

The kind of reentrancy doesn't matter. Neither will work.

 


@Worle wrote:

Any recommendations on how to do this without resorting to restructuring the reentrant VI and storing all of its data in the class instance.


Fundamentally, that is the only way...

 


@Worle wrote:

If I put this in one of my class VI's (even if that is set to preallocated as well) it appears to share the same instance of PID.vi amongst all of the class instances.


Yes. That is an accurate breakdown of the problem.

 

PtByPt is useless for classes. I keep mentioning this, nobody really seems to care.

 

You can:

1) make your own PID with inputs\outputs for it's state (e.g. not PtByPt style).

2) have each class instance create a VI reference to a PID VI instance, and use Call By Reference on that.

 

I don't know which I dislike more 🙄... I usually pick 1), which is more work, but less of a hack.

 

Sorry... Can't make it better.

Message 2 of 5
(981 Views)

That's a shame, although not entirely unsurprising.

 

Thanks for the confirmation 

Message 3 of 5
(973 Views)

@Worle wrote:

Am I correct in thinking that you cannot use a preallocated reentrant vi in a class and expect a new instance of the VI for each class instance? Any recommendations on how to do this without resorting to restructuring the reentrant VI and storing all of its data in the class instance.

 

My use case is, I have a load of heaters which I was considering creating a class for. One of my methods would be set duty cycle which would use the PID.vi (Preallocated reentrant). If I put this in one of my class VI's (even if that is set to preallocated as well) it appears to share the same instance of PID.vi amongst all of the class instances.


The data can also be stored in a separate class, it doesn't have to be in the class using this function. That way, you could use composition to use the same functionality in other classes / outside of the class if needed.

I would suggest making the "function" into the method of a class which is then stored as part of the data of the calling class. Composition versus inheritance.

Message 4 of 5
(946 Views)

@Intaris wrote:

@Worle wrote:

Am I correct in thinking that you cannot use a preallocated reentrant vi in a class and expect a new instance of the VI for each class instance? Any recommendations on how to do this without resorting to restructuring the reentrant VI and storing all of its data in the class instance.

 

My use case is, I have a load of heaters which I was considering creating a class for. One of my methods would be set duty cycle which would use the PID.vi (Preallocated reentrant). If I put this in one of my class VI's (even if that is set to preallocated as well) it appears to share the same instance of PID.vi amongst all of the class instances.


The data can also be stored in a separate class, it doesn't have to be in the class using this function. That way, you could use composition to use the same functionality in other classes / outside of the class if needed.

I would suggest making the "function" into the method of a class which is then stored as part of the data of the calling class. Composition versus inheritance.


Yes, indeed. Same goes for the 'VI Server reference' solution. You can make a PID class that opens a reference to the PID PtByPt VI, put the ref in it's private data and use that class.

 

I prefer to not work by reference, but it's still an option...

0 Kudos
Message 5 of 5
(918 Views)