LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a static class?

Solved!
Go to solution

Hi,

 

simple as it sounds: How can I achieve something like

public static class mystaticclass

so as to prohibit instantiating this class more than once or copying the class and its place in memory by wire junctions. The reason I ask is I want to make sure that no matter where the class wire comes from, it shall always point to the one originally instantiated singleton of this class.

 

My workaround would be a LV2-style global to store the class in but somehow I think this could become somewhat cumbersome over time...

 

Thanks for your advice.

Falk

0 Kudos
Message 1 of 6
(2,728 Views)
Solution
Accepted by topic author comrade

What you are looking for is called "Data Value Reference". You find it in the Application Control-> Memory Control palette.

ʍolɟɐʇɐp ʞuıɥʇ
Message 2 of 6
(2,713 Views)
Solution
Accepted by topic author comrade

The real question is how to avoid the need for it.

Message 3 of 6
(2,695 Views)

Dear Wiebe and crabman,

 

thanks for your quick answers - I certainly can see both of your points and how/why these are rather the ways to do this (or to exactly not do this Smiley Wink ). Therefore I get the gist that my conceptual approach lacks expertise to begin with. As I am only "fixing stuff" in a rather complex piece of software under a bit of time pressure I will for now have to stick to the approach I started so as to keep the changes minimal and at a level I currently can easily wrap my mind around. Be sure, however, I'll save your smarts for later work, guys. Thanks a lot for you help. Smiley Happy

0 Kudos
Message 4 of 6
(2,665 Views)

Adding to\refactoring an existing (problematic) project is hard.

 

As a note on the DVRs. There are two ways to do this. You can put the DVR in the class. This will result in a by reference class. This is not a singleton! You would still need the wire to get the valid (initialized) DVR reference. The need for the wire is actually a good thing, but if you're looking for a singleton, it's only part of the trick. You could put the DVR in a private class global, and then you will have a singleton. Note that the class inputs (class in\out) will be redundant...

 

The second approach is to make the class 'by wire'. Then, make a parent, and a 2nd child that embeds the by wire class in a DVR. You'll still have the by wire class, the DVR class is just a wrapper. I usually try to factor out the by reference, and this way it is less work, and less risky.

 

Some people like to make every class by reference. I don't, but suite yourself. Mixing them will definitely be confusing. If you do like by reference, I'd look into one of those toolkits that support it (G#?). Of course if your application needs one or two by reference classes, you can still make them manually.

Message 5 of 6
(2,599 Views)

Hi Wiebe,

 

thanks for your unbiased thoughts and insights on that issue. I read some of the discussions about whether or not DVRs+IPE are the best way to handle classes etc., here @ni.com as well as over @LAVA.org - it seems to me rather a philosophical question to answer, so everybody diving into this has to choose the paradigm that best fits their programming style. It feels a bit like the ancient Global vs. LV2-Global-discussion again, only this time intellectually on steroids, as OOP is, I think a bit more than just one level above standard LabVIEW programming as I learned it decades ago...

 

As for refactoring the existing project - I take it on one problem at a time. I suppose I'll be a lot greyer (either in mind or hair-wise or rather both) when this is done.

Message 6 of 6
(2,574 Views)