LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Variant to String problem

Hi,
I'm building an application where I need to read values of shared variabels. The problem is that there are many different types of shared variables(boolean,string and double). So I need to fetch values as variants. How can I convert them to string that I could use "Write key" VI to write values as a string to file? I want to use string because booleans and doubles can also be written as strings.I searched for a solution whole day but nothing worked. 
 
Mika
Hope the picture can explain it a bit better.
0 Kudos
Message 1 of 21
(22,676 Views)
To convert a variant to a specific datatype, wire a constant (or control) of that datatype to the type input. The LabVIEW Help describes this function.




Message Edited by smercurio_fc on 04-15-2008 09:08 AM
Message 2 of 21
(22,648 Views)
Hi,
Thanks for Your reply.  I have tried that and the result is that I get an error message " ERROR 91 occured at Variant to Data. The data type is not compatible with the data type wired to the type input."
 
MIka
0 Kudos
Message 3 of 21
(22,620 Views)
Smercurio, I think his problem is that he is converting different types of data to a variant before passing it into the shared variable.  That one time it could be string, another time something else.
 
Mika, in the instance you are getting this error, what kind of data are you putting into the shared variable?  I'm guessing it is NOT a string in that instance.  I would recommend describing your application a little bit more and why you have the need to pass different data types through the same shared variable.  There may be a better way to accomplish what you are trying to do. 
 
You might need to have another, related shared variable where you pass an enum describing each of your possible datatypes.  Use that as a selector to a case structure where each case has a different data type going into the variant to data function.  You may even be able to do this using some builtin LabVIEW functions that can inspect the datatype of the variant.  I want to say there were nuggets on this, but I had a hard time finding one that describes exactly what I am thinking of.  But even if all of this is what you want to do, what do you do with the data once you have it.  In a Boolean case you'd have to connect it to a boolean wire, string case a string wire, numeric a numer wire.  They would have have to different destinations anyway.
 
Maybe I'm reading your original message wrong and you don't have a single shared variable that is a variant type, but numerous shared variables.
 
 I would recommend describing your application a little bit more and why you have the need to pass different data types through the same shared variable.  There may be a better way to accomplish what you are trying to do. 
0 Kudos
Message 4 of 21
(22,614 Views)


Ravens Fan wrote:
Smercurio, I think his problem is that he is converting different types of data to a variant before passing it into the shared variable.  That one time it could be string, another time something else.
 
Mika, in the instance you are getting this error, what kind of data are you putting into the shared variable?  I'm guessing it is NOT a string in that instance.  I would recommend describing your application a little bit more and why you have the need to pass different data types through the same shared variable.  There may be a better way to accomplish what you are trying to do. 
 
You might need to have another, related shared variable where you pass an enum describing each of your possible datatypes.  Use that as a selector to a case structure where each case has a different data type going into the variant to data function.  You may even be able to do this using some builtin LabVIEW functions that can inspect the datatype of the variant.  I want to say there were nuggets on this, but I had a hard time finding one that describes exactly what I am thinking of.  But even if all of this is what you want to do, what do you do with the data once you have it.  In a Boolean case you'd have to connect it to a boolean wire, string case a string wire, numeric a numer wire.  They would have have to different destinations anyway.
 
Maybe I'm reading your original message wrong and you don't have a single shared variable that is a variant type, but numerous shared variables.
 
 I would recommend describing your application a little bit more and why you have the need to pass different data types through the same shared variable.  There may be a better way to accomplish what you are trying to do. 


Hi Ravens.
 
"I think his problem is that he is converting different types of data to a variant before passing it into the shared variable." No,that is not what I am doing. I am trying to open shared variable and read its value. Then I need to write it to text file as a value of same named key. You see, I already have VI that writes values from text files to shared variables. And it works eventhought all values are written as strings not depening on variables data type. So it seems that in double and boolean typed variables you can write values as strings? I attach here the picture of VI that opens a text file, fetches a keyname, opens same named variable and writes keys value to variables value. 
 
 
Mika
0 Kudos
Message 5 of 21
(22,604 Views)
HI again,
 
I read your message again and noticed this   "I would recommend describing your application a little bit more and why you have the need to pass different data types through the same shared variable.". There are about 90 different variables, not only one,.And some variables are boolean type,some doubles and some strings .So  different data types are NOT passed trough SAME variable.
 
The problem is that we don't know beforehand which type it is.I could do some spreadsheet or file that first defines what type that variable is going to be depending on key name. I just don't consider that as a good code, because it would make it a lot harder to expand the application later if it is necessary. At the moment expansion would only demand that user adds variables and same named keys to reference file.
 
Mika
0 Kudos
Message 6 of 21
(22,594 Views)

OK.Problem solved.I used reference files keys values to define what type variables are going to be(d=double,b=boolean,s=string). I didn't notice it earlier but I don't need reference files keyvalues anywhere else. Simple as that. 😉

Mika

 

0 Kudos
Message 7 of 21
(22,569 Views)
I'm glad you got it figured out.Smiley Happy  With your later descriptions, what you are doing makes more sense than the way I was interpreting the original post.
 
Out of curiosity, which LV version are you using?  What is the function in your picture that looks like a piece of paper and has the file path going into it?  I hadn't seen that one before.  Is it a LV subVI or a subVI you made?  I'm wondering if it is an older version of a config file VI.
0 Kudos
Message 8 of 21
(22,538 Views)
Mika,

You need to convert it to the correct data type. This is a bit problematic,
since you might not now the type. You need to convert the variant to string,
if that results in an error, convert it to int, if error, try boolean, etc
(order is arbitrary, put the most used first for speed).

Unfortunatelly, there is still no way to get the string displayed in the
variant control in LabVIEW... Perhaps there are some OpenG tools, or perhaps
something on LAVA... I'd give it a shot before trying it myself. If you're
lucky, you might even find some "variant to ini file" functions.

Regards,

Wiebe.


Message 9 of 21
(22,523 Views)
You might be able to glean the datatype information using the Variant to Flattened String function and looking at the type string output. The information is based on LabVIEW 7, but it might work.
0 Kudos
Message 10 of 21
(22,514 Views)