"The value of the local variable xxx is not used"

Hi,

In my rule I initially had this:

val in_UDPmsg = Tex_UDP_Receive.state.toString.substring(0, Tex_UDP_Receive.state.toString.length)

This works fine.

However, I wanted to add an if statement. So it became this:

	if (Tex_UDP_Receive.state.toString.length > 6 ) {
		val in_UDPmsg = Tex_UDP_Receive.state.toString.substring(0, 6)
	} else {
		val in_UDPmsg = Tex_UDP_Receive.state.toString.substring(0, Tex_UDP_Receive.state.toString.length)
	}

But this generates a lot of errors:

2017-05-24 14:33:11.174 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'texecom.rules', using it anyway:
The value of the local variable in_UDPmsg is not used
The value of the local variable in_UDPmsg is not used
2017-05-24 14:33:11.175 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'texecom.rules'
2017-05-24 14:33:19.597 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Alarm interface via UDP': An error occured during the script execution: The name 'in_UDPmsg' cannot be resolved to an item or type.

I don’t understand why, since in_UDPmsg is defined?

I also tried this:

	val in_UDPmsg = Tex_UDP_Receive.state.toString.substring(0, Tex_UDP_Receive.state.toString.length)	
	if (Tex_UDP_Receive.state.toString.length > 6 ) {
		val in_UDPmsg = Tex_UDP_Receive.state.toString.substring(0, 6)
	}

This is essentially the same. I one get an error message when my rules-file is saved, but not when the rule is being executed:

2017-05-24 15:26:56.161 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'texecom.rules', using it anyway:
The value of the local variable in_UDPmsg is not used
2017-05-24 15:26:56.162 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'texecom.rules'

What am I doing wrong?

omit the second ‘val’, you may not redeclare the variable (or actually it’s a constant you’re using - use ‘var String’ instead of ‘val’ in the first line…)

@mstormi is right, but here is the explanation for why.

In programming, there is a concept of declaring a variable and defining a variable. To declare a variable is to bring that variable into being and give it a name. To define a variable is to give that named thing a value. Often one does both steps on the same line but that is not required.

The keywords val and var are used to declare a variable. The = is used to define the variable. The difference between the two is once you define a variable declared with val you cannot reassign a new value to that variable. It is a constant. Whereas a variable declared with var can be reassigned. Because one cannot reassign a val, one must also define it on the same line as it is declared.

One only declares a variable once in any given context (i.e. between then and end, between { }, or between [ ]).

Therefore, you should declare in_UDPmsg outside your if statements. The { } defines a context and anything declared inside the { } disappear outside the { }.

So your code as written says: if the String is more than 6 characters declare and define in_UDPmsg and then throw it away. Otherwise, declare and define in_UDPmsg and then throw it away.

After the } in_UDPmsg no longer exists because you are outside your declared context and since you never actually used it the log is warning you that this is probably something you don’t want to do with its:

The value of the local variable in_UDPmsg is not used

And then, later on in your rule, you try to use in_UDPmsg and because it no longer exists you get that error about it not being defined.

So in your case, you want to declare a variable name in_UDPmsg and have the option to change its value later. So you want to use var and only use var once because you only declare a variable once. The code becomes:

var in_UDPmsg = Text_UDP_Receive.state.toString.substring(0, Tex_UDP_Receive.state.toString.length)
if(Tex_UDP_Receive.state.toString.length > 6) {
    in_UDPmsg = Tex_UDP_Receive.state.toString.substring(0,6)
}

// do stuff with in_UDPmsg

Having said that, see my previous just posted response on the other thread and look at some of what you can do with split and contains. Also, see the String JavaDoc for everything you can do with a String.

2 Likes

Hi @mstormi and @rlkoshak,
Thank you both for your help!
Rich, thanks for the detailed explanation as well. I wasn’t aware of this. I just checked if all my rules were in line with this approach. That happens when you borrow someone’s code without fully understanding all of the complexities. :slight_smile:

Dries