Computer Science Canada Record types in classes |
Author: | spongeboob [ Sat Jun 03, 2006 5:12 pm ] | ||
Post subject: | Record types in classes | ||
The code returns the error "Assigned value is the wrong type" How can it be fixed so that it works? |
Author: | TheOneTrueGod [ Sat Jun 03, 2006 5:19 pm ] | ||
Post subject: | |||
Just because they are named the same, doesn't mean they are the same thing ![]()
|
Author: | spongeboob [ Sat Jun 03, 2006 5:37 pm ] |
Post subject: | |
![]() but what if the class is a different file and I want import it How would I declare the type as a "global" then? |
Author: | TheOneTrueGod [ Sat Jun 03, 2006 5:55 pm ] |
Post subject: | |
Well, off the top of my head, you could probably put it in a module in the other file (I'm not sure if this would work or not) and use it in your main code and in the other file. You'd have to test it out though. |
Author: | Cervantes [ Sat Jun 03, 2006 6:02 pm ] | ||||
Post subject: | |||||
Your type is global. It is not, however, pervasive. This means that you have to manually import it into classes and modules. If you want to make it pervasive, define it with the keyword pervasive or with an asterisk (*). Here's an example for creating a pervasive variable.
Similarly for a type. If the class is in a different file and you want to import it, add the keyword unit to the start of that file and make sure that file only contains the class definition. Save the file as "<class name>.tu". Then in your main program,
at the start of the code. |
Author: | spongeboob [ Sat Jun 03, 2006 6:27 pm ] | ||||
Post subject: | |||||
So this is what I did "testType.tu"
"whatever.t"
and...it works!! ![]() |
Author: | Cervantes [ Sat Jun 03, 2006 6:54 pm ] |
Post subject: | |
You probably shouldn't be declaring a pervasive variable or type from within a class. The only things that should be pervasive are things that are truly global, like the gravitational constant of the universe. Global variables are typically a bad idea. I recommend you either declare the type outside the class and import it or declare it inside the class and export it. Probably the former. |