Home AMX User Forum NetLinx Studio

Having trouble with FUNCTION

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,

Comments

  • DHawthorneDHawthorne Posts: 4,584
    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.
  • mpullinmpullin Posts: 949
    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!
Sign In or Register to comment.