March 28, 2024, 12:27:27 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Question regarding classes in projects

Started by Guilect, May 25, 2010, 07:46:46 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Guilect

Hi,

having a problem with getting a class to work in a project.

I have attached an example project (compile as console).  The file structure of which I wish to maintain.

The issue is getting the defined class to report the proper value when called from an external subroutine ( in secondary.eba)

Calling a method in the main code file (main.eba) correctly returns the expected value of 44.
However, calling the same method of the same class object from an external subroutine returns a value of 0.

Looking for help.

Thanks in advance.

REDEBOLT

Hi Guilect

First I got rid of "globals.eba".  It only served to confuse me.
Next, I followed Paul's guidelines:
I renamed "class decl.eba" to "class decl.inc".
Class declarations should always be an "inc" file and included into every module which uses the class.
Class definitions should always be an "eba" file.
"myClass" should be defined in the "Main" module.
The "secondary" module defines a global sub.
When the "main" module calls the "externalsub," it must pass the name of the class as a parameter.

My solution is below.
Regards,
Bob

Guilect

Bob,

thanks for taking the time to reply and detail all the changes.
Your methodology works...

I am left however confused why in the original project a globally defined class' method can not be accessed properly in the secondary.eba file.
For examples sake, if I were to declare 50 global variables in main.eba, I would hate to have to pass them all as parameters to
a subroutine in which I want to use them.  It seems to defeat the idea of being global.

Thoughts?....

Regards.

sapero

May 25, 2010, 01:37:00 PM #3 Last Edit: May 25, 2010, 01:41:10 PM by sapero
Your class was allocated twice:
The main.eba module when including globals.eba- allocated one instance and initialized it.
The secondary.eba module allocated it too, but it was not initialized. The externalsub() subroutine was accessing the uninitialized class, thus displaying zero.

You could also move myClass variable to main.eba:
global myClass
def myClass as cClass ' $main must be before this line if you have a constructor or virtual methods in cClass.


And in secondary.eba add:
extern myClass as cClass

Guilect


REDEBOLT

Quote from: Guilect on May 25, 2010, 01:30:22 PM
For examples sake, if I were to declare 50 global variables in main.eba, I would hate to have to pass them all as parameters to
a subroutine in which I want to use them.  It seems to defeat the idea of being global.
Regards.

I think I understand what you are talking about concerning globals.
' main

$INCLUDE "class decl.inc"
GLOBAL myClass
GLOBAL myClass2

$main
def myClass as cClass
def myClass2 as cClass
declare Extern  externalsub()

' set foo to 44
myClass.setfoo(44)
' prints 44
print myclass.getfoo()

' set foo to 43
myClass2.setfoo(43)
' prints 43
print myclass2.getfoo()

' call same routine as above but now from external SUB
' get 0, not 44 ???
externalsub()

WAITCON


Here, I define myClass and myClass2 as global.


' secondary
$INCLUDE "class decl.inc"

EXTERN myClass as cClass
EXTERN myClass2 as cClass

Global sub externalsub()
print myClass.getfoo()
print myClass2.getfoo()
endsub


Then, I declared myClass and myClass2 as extern,
and then used them in the subroutine.

Is this what you were thinking?
Regards,
Bob

Guilect

Hi Bob,

yes that is what I was thinking.
It looks so clear with declaring everthing in the main module.

I tend to have a seperate file for global declares and just have executable code in my main module.
With the way I was keeping all my global variables, in their own file (globals), and then including that file
again in my secondary module, sapero saw that the class was being instantiated a second time and was acting
like an unitiialized local class variable.

So either I can declare all my globals in my main.eba (like you suggest) or I could just move any global class declarations to main.eba, keeping other global declares in their own file.

Thanks again.

LarryMc

I use the globals.eba setup all the time.
For example.
In my Visual Designer of IWBasic I have a globals.eba file that uses
projectglobal "on"
   int blah
projectglobal "off"

It contains over 60 variables.
My project has 34 source files at the current time.
All except the globals.eba have the following line of code:
$include "globals.eba"
One of my source files could be all the source for a class.
If so then the class definition would be included in my project.inc file; the same as any declare imports, and declare externs.

Then I would use def myClass as cClassin whatever source file I needed.

LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Guilect

Hi Larry,

I need to restructure my includes and globals to be like yours.
I am not use to using so many files in a project, but I am getting there. :)

Thanks,
Guilect

LarryMc

If you need any help just post or if need be send me your files.

LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library