LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to create .net constant from a string

Is there a way to generate a .NET class refnum constant from a string? I have a list of strings. Each one is a .NET class. I also have an array of .NET objects, all of the same super class. I want to cast these super objects down to their respective classes. I know I can't do this programatically but I'd like to be able to generate the .NET constants from the list of strings in one VI and then copy them into my VI to do the casting rather than have to select the class of each constant individually by hand. It'd be nice if the 'to more specific class' VI took a string containing the dotted class name as well as a .NET constant of that class.
0 Kudos
Message 1 of 7
(4,333 Views)
I honestly have to say I had to read your post several times. You said "I know I can't do this programmatically", yet you're asking how to do it. I still can't wrap my head around this one. It almost sounds like you're trying to do something like what you do with VBScript which uses the CreateObject function like in "var myObj = CreateObject("classname")". That works because in VBScript all variables are variants. I would turn this question around and ask: can I do this in, say, C#? Granted, I'm not an expert in C#, but I don't see how, because you have to declare variables to be of a certain type, and in order to access the methods and properties the compiler has to know the type. It's the same in LabVIEW. You could treat everything as a variant, but that won't get you too far.

Perhaps you need to turn the problem around and use classes. In other words, create LabVIEW classes which are wrappers for your various .NET classes and then use the appropriate LabVIEW class based on your string.
0 Kudos
Message 2 of 7
(4,314 Views)
What I mean to say is, I know I cannot give the 'to more specific class' VI a dynamic type. I have so many 'to more specific class' VIs and I want to avoid having to select the .NET class type of the .NET constants that are wired to each one when I write, or later rewrite, the program. I'd like to be able to create the constants on the block diagram of one program using strings. Then copy and paste those statically typed constants into another program. So it'd be like a set of dynamic .NET constants that become static on the block diagram after they are programatically typed and the program finishes? I'm pretty sure this is impossible but I thought I'd ask.
0 Kudos
Message 3 of 7
(4,309 Views)
I tried looking at whether this can be done using VI scripting, but the .NET Constructor does not seem to be exposed in the LV class hierarchy and the .NET reference control does not seem to have exposed properties or methods to allow this, at least not in LV 7.1.

___________________
Try to take over the world!
Message 4 of 7
(4,286 Views)
Don't know if original poster is still monitoring this thread, but I wanted to post a follow-up with a possible solution. While fiddling around in a C# program I'm writing I realized that you may be able to do this using Reflection. Basically, this involves dealing with the System.Type and the System.Reflection namespace. There's a lot more coding involved, obviously, but you can essentially create .NET objects using the GetType method and then use reflection to get methods and constructors by name and invoke them. Attached is an example (LabVIEW 8.20) that creates a FileInfo class to copy a file. You may be able to extend this to do what you want.
Message 5 of 7
(4,247 Views)
I looked through the VI and it's interesting. I could use this process to call methods and access properties of .NET objects that are programatically defined by strings. Though the generic object and type classes would have to be carried through the whole program. The only other way I can see at this point would be to create a VI with however many generic .NET constants I wanted on the block diagram, save it, then, in another VI, open the first VI as a text file and replace every instance of assembly.namespace.generic object with a string which has the class I want and then saving the VI. However, doing this results in LabVIEW saying the VI is of a later version and cannot be opened after it is modified as a text file. Soooooo, anyone happen to have the proprietary, confidential, super secret .VI file format 🙂
0 Kudos
Message 6 of 7
(4,230 Views)


@ayd,lfud. wrote:
The only other way I can see at this point would be to create a VI with however many generic .NET constants I wanted on the block diagram, save it, then, in another VI, open the first VI as a text file and replace every instance of assembly.namespace.generic object with a string which has the class I want and then saving the VI. However, doing this results in LabVIEW saying the VI is of a later version and cannot be opened after it is modified as a text file. Soooooo, anyone happen to have the proprietary, confidential, super secret .VI file format 🙂

That sounds like a bad idea.

a) The VI format can change between different versions (e.g. 7.1 to 8.0).
b) A VI has a binary format where presumably the structure changes based on the content and changing it would\could corrupt the VI. In this case, it looks like LV thinks the VI was saved in a newer version because it looks for a certain piece of info (the version the VI was saved in) and finds something it doesn't understand.

What you want might be as easy as finding the length of each name (should *probably* be the four bytes before the string, since that's how LV stores strings) and changing it when you change the name, but it might also not be that. I might try it just to see if it works, but I don't think I would rely on it.


___________________
Try to take over the world!
0 Kudos
Message 7 of 7
(4,189 Views)