Having trouble with FUNCTION
fogled@mizzou
h4x354x0r
I've mostly been using DEFINE_CALL for subroutines in my project so far, but I've got a place where I think a function would be much better. But I'm still apparently having data typing problems. Here's the relevant code snippets:
Thanks,
DEFINE_FUNCTION CHAR get_device_text (active_device)
{
CHAR device_text[25]
if(active_device == cpu) { device_text = "'Lectern Computer'" }
else if(active_device == aux) { device_text = "'Lectern Guest Laptop'" }
// More iterations of if statements
else { device_text = "'UNDEFINED'" }
RETURN device_text
}
... but of course I get a compiler warning "Converting type [string] to [CHAR]" with this syntax. I obviously just don't quite "get it" with the data type or something else with the way functions handle data typing. Can someone please set me straight?Thanks,
Comments
-
fogled@mizzou wrote:I've mostly been using DEFINE_CALL for subroutines in my project so far, but I've got a place where I think a function would be much better. But I'm still apparently having data typing problems. Here's the relevant code snippets:
DEFINE_FUNCTION CHAR get_device_text (active_device) { CHAR device_text[25] if(active_device == cpu) { device_text = "'Lectern Computer'" } else if(active_device == aux) { device_text = "'Lectern Guest Laptop'" } // More iterations of if statements else { device_text = "'UNDEFINED'" } RETURN device_text }... but of course I get a compiler warning "Converting type [string] to [CHAR]" with this syntax. I obviously just don't quite "get it" with the data type or something else with the way functions handle data typing. Can someone please set me straight?
Thanks,
If I am understanding what you are doing correctly, your active_device parameter in the function definition has no variable type, so it goes to the default of CHAR; in addition, for it to be a string you need to put an array designation. So make your function definition read: DEFINE_FUNCTION CHAR get_device_text (CHAR active_device[]) , and it should be fine. -
Ok, there are a number of mistakes here.
1) You are returning a character array called device_text but your function's return type is for a single character. You want to change CHAR in the definition to CHAR[25] (you can't return an unbounded array, need to specify a size, in your case, 25).
2) Your function definition accepts active_device but you do not give active_device a data type, so NetLinx will assume it's an integer. However I think you want it to be a string so I'd change (active_device) to (CHAR active_device[]).
3) You're trying to create a variable to use within this function (device_text). You need to define it as either a LOCAL_VAR or a STACK_VAR. I'd say you want a STACK_VAR here. So make it STACK_VAR CHAR device_text[25].
4) You use tests such as if(active_device == cpu) and if(active_device == aux). Unless cpu and aux are variables you defined globally this won't work. In other to be compared as strings you need single quotes e.g. active_device == 'cpu'
5) Unless you're building a string with a variable as part of the string (or passing a string to a function that expects one) you don't need the double quotes, so I removed them.
6) You could save typing by using a switch-case instead of repeated else-if structures but this isn't strictly speaking necessary so I didn't change that.
Here's the result:DEFINE_FUNCTION CHAR[25] get_device_text (CHAR active_device[]) { STACK_VAR CHAR device_text[25] if(active_device == 'cpu') { device_text = 'Lectern Computer' } else if(active_device == 'aux') { device_text = 'Lectern Guest Laptop' } // More iterations of if statements else { device_text = 'UNDEFINED'} RETURN device_text } -
mpullin wrote:Ok, there are a number of mistakes here.
1) You are returning a character array called device_text but your function's return type is for a single character. You want to change CHAR in the definition to CHAR[25] (you can't return an unbounded array, need to specify a size, in your case, 25).
2) Your function definition accepts active_device but you do not give active_device a data type, so NetLinx will assume it's an integer. However I think you want it to be a string so I'd change (active_device) to (CHAR active_device[]).
3) You're trying to create a variable to use within this function (device_text). You need to define it as either a LOCAL_VAR or a STACK_VAR. I'd say you want a STACK_VAR here. So make it STACK_VAR CHAR device_text[25].
4) You use tests such as if(active_device == cpu) and if(active_device == aux). Unless cpu and aux are variables you defined globally this won't work. In other to be compared as strings you need single quotes e.g. active_device == 'cpu'
5) Unless you're building a string with a variable as part of the string (or passing a string to a function that expects one) you don't need the double quotes, so I removed them.
6) You could save typing by using a switch-case instead of repeated else-if structures but this isn't strictly speaking necessary so I didn't change that.
Thanks for the primer. This info allowed me to get my function working correctly. Only a couple of notes about the above: active_device is an integer, and the things it tests against (cpu, aux, etc) are defined as integer constants.
And I can't explain why I'm not using CASE statements, I use them in most other places in my code. Just a brain fart, I'll clean it up before deployment.
Thanks everyone!
Categories
- All Categories
- 2.5K AMX General Discussion
- 922 AMX Technical Discussion
- 514 AMX Hardware
- 502 AMX Control Products
- 3 AMX Video Distribution Products
- 9 AMX Networked AV (SVSI) Products
- AMX Workspace & Collaboration Products
- 3.4K AMX Software
- 151 AMX Resource Management Suite Software
- 386 AMX Design Tools
- 2.4K NetLinx Studio
- 135 Duet/Cafe Duet
- 248 NetLinx Modules & Duet Modules
- 57 AMX RPM Forum
- 228 MODPEDIA - The Public Repository of Modules for Everyone
- 943 AMX Specialty Forums
- 2.6K AMXForums Archive
- 2.6K AMXForums Archive Threads
- 1.5K AMX Hardware
- 432 AMX Applications and Solutions
- 249 Residential Forum
- 182 Tips and Tricks
- 146 AMX Website/Forums