LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to use preprocessor directives as in C?

I have a list of id's and corresponding names associated with them,i want to map the id's to the names during the first time program runs and to use the names after that instead of id's during run time(but not mapping the id to the name again and again during runtime,only once during the start of the program, something like preprocessor directives in c ) can this be done in labview.
0 Kudos
Message 1 of 10
(3,759 Views)

a preprocessor directive such as #def is similar to using typedefs in c.  It is not quite as flexible as c but easy to use.  Sounds like you want to make an enum, name the ids and then use the enum instead of the number.  Create a custom control, make an enum, edit the enum to add the items and make sure you save it as a typdef.  Now use this typdef throughout your program instead of the id.  if you update the typdef to add elements the changes will automatically update throughout the application.  The one major caveat is that unlike enums in c you can not skip indexes for sparse enums  nor can you change this list at runtime.  But you will make coding much easier just as in c you are defining your ids external to your code. 

 

Paul

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 2 of 10
(3,744 Views)
If you need arbitary numbers (not sequential) you can use a ring instead. But it is less 'powerful' than the enum.

Felix
0 Kudos
Message 3 of 10
(3,741 Views)

Yes rings can be sparse and can also be changed at runtime, the disadvantage is that they dont autopopulate case structures and the strings[] are not as accessable as enums.  Too bad NI has not gotten around to making a sparse enum.  I sometimes use 'undefined #' for enum cases to make a sparse enum but this is not all that elegant.

 

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 4 of 10
(3,731 Views)
If an enum needs numeric values, another option is embedding them in the enum text (e.g. separated by a colon) and parse them out in a small subVI. Wiring the enum into a Format Into String primitive will return the enum text.

___________________
Try to take over the world!
0 Kudos
Message 5 of 10
(3,710 Views)
Actually i want to implement in the reverse way
i want the ids to be mapped to the name rather than name to the ids
so i think enum will not work as it only takes number(in different formats as the value)
 
Can code in c as below can be implemented in Labview
 
#define a 50
#define b 10
 
int main()
{
 
   int ch;
  while(1)
{
   scanf("%d",&ch);
   switch(ch)
   {
     case a: printf("Hiiiii");
     break;
     case b: printf("Hello");
     break;
     default:
   }
   
  }
}
 
suppose
first input is ch=50
then output should be Hiiii
next input is ch=10
then output should b Hello
 
Can this be realized in Labview
0 Kudos
Message 6 of 10
(3,691 Views)

That depends on how you want to input the integer, but here's one way, which assumes that the user is selecting it from a closed list.

Note that in this case the value of the ring (0 or 1) corresponds to your a and b.



Message Edited by tst on 01-25-2008 12:25 PM

___________________
Try to take over the world!
0 Kudos
Message 7 of 10
(3,682 Views)

And you can do it even simpler if you use a combo box:


___________________
Try to take over the world!
0 Kudos
Message 8 of 10
(3,664 Views)

I would do #define as a simple vi that returns a value.  I do this all the time to define constants throughout programs.  I know there is more overhead than your #define preprocessor but it is a clean implementation and the overhead is low when you consider the speed of a current PC.

 

Paul

Paul Falkenstein
Coleman Technologies Inc.
CLA, CPI, AIA-Vision
Labview 4.0- 2013, RT, Vision, FPGA
0 Kudos
Message 9 of 10
(3,627 Views)

Hi Amit,

      A G implementation of "#define" seems to imply pre-compile graphical substitutions - that would be interesting.  Of course it's not supported and a work-around seems necessary.  I began reading this thread with the same enthusiasm for Enums as tst, and Falkpl, but at this point Enums seem unsatisfactory, expecially in light of your criteria of "not mapping the id to the name again and again during runtime,only once during the start of the program" (that seems to rule-out falkpl's #define VI, but what about tst's idea of parsing the enum's string-representation?)

      In "C", instead of the #define, one might accomplish the very same effect (maintaing values in one place) with [global] constants.  LabVIEW has constants too - they can be defined in one place and wired to wherever they're used - why not do that?  Are you trying to achieve a specific "look" or "effect" on the diagram?

Cheers.

const int a = 50;
const int b = 10;
 
int main()
{
 
   int ch;
  while(1)
{
   scanf("%d",&ch);
   switch(ch)
   {
     case a: printf("Hiiiii");
     break;
     case b: printf("Hello");
     break;
     default:
   }
   
  }
}


Message Edited by tbd on 01-28-2008 01:10 AM
"Inside every large program is a small program struggling to get out." (attributed to Tony Hoare)
0 Kudos
Message 10 of 10
(3,599 Views)