Code Block if statement error on non-executed portion of statement

Forgive me if this has already been discussed. I look but couldn’t find anything similar.

I’ve had several if statements in code blocks that have been giving me warnings that only pertain to the portion of the statement that shouldn’t be running. I try to say if condition is true, do THIS, else do THAT. Then when the condition is false I expect it to only do THAT. Well, it does do THAT, but it also gives me a warning that it can’t do the THIS because of the condition. And usually, I know that it can’t do the THIS on false conditions, and that’s why I am trying to make an if statement.

An example would demonstrate this better. In the image below, I try to get the first 31 characters if a string if the input string is greater than 31 characters. Otherwise, I want the output to equal the input. I pass a 13 character string through the node and it outputs the 13 character string like it’s supposed to, but it also tells me that it can’t get the first 31 characters of the string. I want to tell it, “yes, but I’m not asking you to…” but I don’t know how to communicate this to Dynamo. If I disconnect the node, run the graph, reconnect the node, and run the graph again, the error goes away. The error only shows up the first time I run the graph or when I run it from Dynamo Player (which is how everyone else at my company will typically run it).

What am I doing/thinking wrong? I’m sure there are other ways to do what I’m trying to do in this specific case, but this similar situation keeps occurring on other code blocks so I’m hoping there is a generalized solution.

Unable to replicate your issue (Dynamo version 2.11) (edited)
However, check if an Imperative approach works for you.

fileName;
a = [Imperative]
{
	 if (String.Length(fileName)>31)
	 {
	 	return String.Substring(fileName,0,30);
	 }
	 else
	 {
	 	return fileName;
	 }
};

Thanks! I don’t get the error when I use the imperative block. I don’t think I fully understand the difference and the imperative block syntax is less intuitive to me, but I guess I’ll go with it for now.

That seems strange that you don’t get the error (and I don’t get it if I disconnect, run, reconnect run). I’m using Dynamo Core 2.3.0.5885 and Dynamo Revit 2.3.0.8352. I’ll try to see if others at my company get the same error, or if I’m the only one.

1 Like