patrickokeeffe Posted November 16, 2010 Share Posted November 16, 2010 I've created a class with local variables and a few functions but the functions are unable to access the variables. It wasn't clear I was doing it right so I copied the following from the OOP tutorial into a new sequence and tried to make it work: class Person local string name = "name" // added = "name" local string address = "address" // added = "address" local string zip = "zip" // added = "zip" function OnCreate() // added this function ? "onCreate" // results in two "onCreate" printed PrintMe() // doesn't print anything or cause errors endfunction function OnDestroy() // added this function ? "onDestroy" // results in two "onDestroy" printed endfunction function PrintMe() ? name ? address ? zip endfunction endclass class PersonPhone parent Person local string phone function PrintMe() ? name ? address ? zip ? phone endfunction endclass People[0] = new(Person) People[0].name = "Frank Smith" People[0].address = "123 Main St" People[0].zip = "12345" //People[0].PrintMe() // causes C1000 error //? People[0].name // causes C1000 error People[1] = new(PersonPhone) People[1].name = "George Johnson" People[1].address = "123 Charles St" People[1].zip = "54321" People[1].phone = "555-1212" //People[1].PrintMe() // causes C1000 error ? "Still alive?" // printed between onCreate's and onDestroy's //? NumRows(People) // causes C1000 error //for (private.x = 0, x < numrows(People), x++) // causes C1000 error //People[x].PrintMe() //endfor My results were very different than expected. Using the original code results in C1000 errors; run as-is the sequence outputs: onCreate onCreate Still alive? onDestroy onDestroy Does anyone understand what's wrong here? I'm using Base Release 5.84 Build 1636 Link to comment Share on other sites More sharing options...
AzeoTech Posted November 17, 2010 Share Posted November 17, 2010 Its because you nevere declared the "people" variable. new() is still executing, but basically the result is being assigned to nothing because "people" doesn't exist. DAQFactory doesn't generate errors when you try and assign to something that doesn't exist. However, it does throw errors when you try and call a function that doesn't exist, and that's why you get the C1000 error when you call PrintMe(). Add "private people" before all the assignments and it will work. Two other points: 1) OnCreate is limited in what you can do because its called before the object fully exists, thus the reason you don't see output when you try and call printMe. I actually prefer to create my own Init() function so I can control it. 2) Objects aren't necessarily destroyed when you think. DAQFactory's garbage collection runs async, so just because all references to an object go out of scope, doesn't mean the object is going to be destroyed right away. So, you may see a delay before OnDestroy is called. Link to comment Share on other sites More sharing options...
patrickokeeffe Posted December 7, 2010 Author Share Posted December 7, 2010 Ok, I modified the code to include a declaration and exclude the onCreate/onDestroy events but it still gives an error accessing a variable and, oddly, still printed my onCreate/Destroy messages. class Person local string name local string address local string zip function PrintMe() ? "Name: "+name ? "Addr: "+address ? "Zip: "+zip endfunction endclass class PersonPhone parent Person local string phone function PrintMe() ? "Name: "+name ? "Addr: "+address ? "Zip: "+zip ? "Ph #: "+phone endfunction endclass private people People[0] = new(Person) People[0].name = "Person One" People[0].address = "123 Main St" People[0].zip = "12345" People[1] = new(PersonPhone) People[1].name = "Person Two" People[1].address = "123 Charles St" People[1].zip = "54321" People[1].phone = "555-1212" for (private.x = 0, x < numrows(People), x++) ? "Person "+doubletostr(x) People[x].PrintMe() endfor Here is the command window output after clearing the global space and running the script twice: clearglobals()onCreate onCreate Person 0 Name: Person One Addr: 123 Main St Zip: 12345 Person 1 Name: name Addr: address Zip: zip 12/07/10 11:50:46.557 C1086 One of the parameters was empty: Line 5: OOPtest Line 39 - Uncaught error in sequence OOPtest onDestroy onDestroy onCreate onCreate Person 0 Name: Person One Addr: 123 Main St Zip: 12345 Person 1 Name: name Addr: address Zip: zip 12/07/10 11:51:14.739 C1086 One of the parameters was empty: Line 5: OOPtest Line 39 - Uncaught error in sequence OOPtest onDestroy onDestroy Completely restarting DAQFactory finally stopped the onCreate/onDestroy messages and also caused the three lines after "Person 1" containing values of "name", "address", and "zip" to stop appearing (which makes me wonder about garbage collection); it did not solve the C1086 error, however. Thinking it could be related to objects inside arrays, I made the code simpler: class Person local string name local string address local string zip function PrintMe() ? "Name: "+name ? "Addr: "+address ? "Zip1: "+zip endfunction endclass class PersonPhone parent Person local string phone function PrintMe() ? "Name: "+name ? "Addr: "+address ? "Zip2: "+zip ? "Ph #: "+phone endfunction endclass private people private peeple People = new(Person) People.name = "Person One" People.address = "123 Main St" People.zip = "12345" Peeple = new(PersonPhone) Peeple.name = "Person Two" Peeple.address = "123 Charles St" Peeple.zip = "54321" Peeple.phone = "555-1212" ? "Person 1" People.PrintMe() ? "Person 2" Peeple.PrintMe() And it still fails: Person 1Name: Person One Addr: 123 Main St Zip1: 12345 Person 2 12/07/10 12:22:13.126 C1086 One of the parameters was empty: Line 2: OOPtest Line 59 - Uncaught error in sequence OOPtest I'm not sure why this child class doesn't seem to inherit from its parent but I'm developing a class with children and am having parallel problems getting variables to inherit, or at least accessing local inherited variables in member functions. Can anyone identify some changes that will make this OOP test code work? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.