Class Function Problems


Recommended Posts

Trying to get started on some production code, but keep running into problems suggesting there are things I'm still not clear about. Thought I was following suggestion to put multiple functions in a class if I didn't want a separate sequence for each function. So generated the following class with a little test function TimesTwo.

 

 

class genclass

function TimesTwo(x)
   return x * 2
endfunction

//--- Save & Read Json Data
function JsonSave(string filename, obj)
   try
      private h = file.Open(filename + ".jsn", 0, 1, 0, 0)
      file.Write(h, obj.toJson())
      file.Close(h)
   catch()
//      ? strLastError
   endcatch
endfunction


function JsonRead(string filename, obj)
   try
      private h = file.Open(filename + ".jsn", 1, 0, 0, 0)
      private string jsondata = file.Read(h, file.GetLength(h))
      file.Close(h)
      obj.fromJson(jsondata)
      return null
   catch()
//      ? strLastError
   endcatch()
endfunction

endclass

global gen = new(genclass)

 

After starting the sequence on the command line entering ? gen returns Object(s)

 

But after entering ? gen.timestwo(5) I get Syntax Error, a single character line is not valid - Error compiling sequence timestwo, Line Number 3

 

What am I missing?

 


 

Link to comment
Share on other sites

Learning a new language is always a little tricky, especially with the syntax.  It is, sometimes, even worse if you know a similar language (like C in this case) as old habits fall in.  In your case, I believe the problem is that you don't have parenthesis around the thing you want to return.  It should be:

 

return(x*2)

 

C / C++ will take it either way.  DAQFactory will not.  You have to use () in your return statements.  Same goes with new().

Link to comment
Share on other sites

I think the problem has more to do with the syntax for defining passed parameters to function. This seems to be confirmed by the error message line number.

The jsonsave and jsonread functions do not return anything and they are not working either. Same error message different line numbers. I tried the following and it worked.

 

class testclass

   function TimesTwo(x)
      return x * 2
   endfunction

endclass

global test = new(testclass)

 

This setup as a separate sequence actually works without return (x *2) syntax.

 

I went back to your examples on OOP. Initially I set up a sequence without "header" and it worked. Then I added header to the function and got the same error message. Because it was saying something about single line character in the error message I tried adding the line now commented out // private string header. Then it seemed to work. Then I commented out the // private string header line and still seems to work.

 

// Test class

class Person
   local string name
   local string address
   local string zip
   
   function PrintMe(string header)
   //   private string header
      ? header
      ? name
      ? address
      ? zip
   endfunction
   
   // Create new object
   function OnCreate()
      name = "not entered"
      address = "not entered"
      zip = "not entered"
   endfunction
   
endclass

global Jimmy = new(Person)

Jimmy.name = "James"

 

At one point I tried putting the JsonSave() and JsonRead() functions in a sequence outside of any classes with an empty Junk() function at the top. The sequences worked in that case only if I called them from the command line with a "?" in front of the JsonSave() and JsonRead() functions which doesn't make sense to me since they don't return anything. At one point I did have them returning the error message if any. Is the older version of the function, getting stuck in memory until a new one will compile?

Link to comment
Share on other sites

You are right, it is acting quite weird.  I was able to create a separate sequence and copy each function individually to a different class and it worked fine.  My thought is that there is some hidden character in the sequence that the compiler is complaining about, but I'll have to check a little later.

Link to comment
Share on other sites

Think maybe I found something helpful. I took the same sequence and tried removing the error catching. No change. Then I tried indenting all of the functions and the lines within them with a tab. Now TimesTwo() works and so does the JsonSave() and JsonRead() functions. Is this the problem, the compiler can't handle code unless everything is indented just right?

Link to comment
Share on other sites

Don't have everything figured out. Tried moving the functions back into a sequence without a class and now compiler can't seem to find file functions.

 

By the way I've noticed some code posted here that is colored coded and looks like it came right from the DF editor. What's the technique for doing that as opposed to attaching screen captures like I have been doing?

Link to comment
Share on other sites

Whatever it is, is probably pretty subtle.  I'm generally pretty good about indenting, but if I'm evolving something and want to test before formatting, or just dashing off something quick and temporary, I might not, and I've never had any issues I was able to trace to indentation or lack of it.

Link to comment
Share on other sites

In trying to work through all of the issues discussed here it seems like the problems are somewhat hit and miss (doesn't work, changes indentation then it does work), I wondering now if the problems might be software vs. OS. I'm doing my work on Windows 8.1 because we expect to deploy to customers on that OS.

 

How many other DAQFactory users are on Win 8?

 

Is anyone else making extensive use of script on Win 8 and having problems?

Link to comment
Share on other sites

No, its not the OS.  I'm on Win7x64 and see the same issue.  As I said, we can reproduce it, so will be able to chase down the problem.  As far as I know, based on the number of times I get asked if DAQFactory runs on Win8, there are a lot of user's on Win 8.

Link to comment
Share on other sites

OK, found the problem.  You were right, it was the indenting, or at least partially so.  It was a +1 error.  When we are isolating the function during compilation, we were grabbing one extra character on the endfunction line, resulting in a line that says simple "e".  If you indented endfunction, then that line is a single space, which the compiler ignores, but if endfunction isn't indented, it picks up the e.  We've fixed the problem and I can either get you a fresh executable, or you can wait until the next release and simply make sure your endfunction's are indented with a space or two.

Link to comment
Share on other sites

This getting odd and inconsistent results. As you can see in the following without the ? before the JsonSave function it generates an error Channel or function not found, and this is on a function without any return value.

post-9196-0-06768800-1395115706_thumb.jp

Link to comment
Share on other sites

Ok, I'm attaching a ctl document for you to look at.

 

If I start all of the sequences, and then try to do a jsonsave("test1", gw) I get an error. If I do ? jsonsave("test1", gw) even though the function is not returning anything.

 

If I try the same thing with TimesTwo(4), I don't get the error even though the function does return a value.

 

The same functions placed into a class (TestSeq2) even without indentation now seem to work.

functest.ctl

Link to comment
Share on other sites

I'm not sure what is happening, and technically having multiple functions in a single sequence like this is not a documented feature, or even one we intended.  I believe the problem is scope.  The parameters exist in the function, but when you call a function like file.open() its looking for those variables in the wrong place (probably in the overall sequence).

Link to comment
Share on other sites

  • 3 weeks later...

Problem with OR in an IF comparison. Thought I had used OR before without problems, but kept getting an error inside of a class with a lot of code. Kept stripping it down and found the problem to be with the OR comparison. I'm using Release 5.90 Build: 2157

 

The attached project has two sequences showing the problem - one with it inside a class, and one just in a sequence.

Problem-Or.ctl

Link to comment
Share on other sites

OR is not a function or operator in DAQFactory.  What you want is:

 

||

 

for a logical OR, or

 

|

 

for bitwise.  In general, operators tend to follow C.  See 4.12.2 for boolean operators.

 

Also, if you don't mind, if your post is off topic, please start a new topic.  It makes it easier for others to find if they have the same question.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.