Shared Variable Not Being Recognized in #Includes

New to FreeBASIC? Post your questions here.
Cjsheaf
Posts: 22
Joined: Jul 08, 2010 4:39

Shared Variable Not Being Recognized in #Includes

Postby Cjsheaf » Jul 01, 2011 2:07

As I've gotten more experienced coding, I've been breaking up my projects into more and more files to help with managing all the code. I recently started an old project over from scratch to try and implement all the new OOP techniques I've recently learned.

I have a main file which contains the main program loop and all of the Shared variables my program will use. I also have a few other "manager" files (basically a Type with several associated functions for retrieving and modifying private data) to control various aspects of the program.

In my main program I have 3 Shared Integers called MouseX, MouseY, and MouseButton which (as you can imagine) store variables related to the mouse. For whatever reason however, I get "error 41: Variable not declared" errors whenever I try to reference any of these in one of my "managers".

The Shared variables are declared in the main file well before I #Include any of the managers, so I have no clue why I can't access them.
Dr_D
Posts: 2389
Joined: May 27, 2005 4:59
Contact:

Postby Dr_D » Jul 01, 2011 3:57

You probably want to look into either the common or extern keyword.
Cjsheaf
Posts: 22
Joined: Jul 08, 2010 4:39

Postby Cjsheaf » Jul 01, 2011 4:03

I see. So I take it that Shared is only for within one code file.

From looking at both, I don't see any reason to use common over extern. Is there any advantage to doing so?
Dr_D
Posts: 2389
Joined: May 27, 2005 4:59
Contact:

Postby Dr_D » Jul 01, 2011 4:18

Well, the biggest difference is this:

FBWiki wrote:Extern only declares variables, arrays and objects, and does not define them (different from Common or Dim). It also has the effect of making symbolname a shared name, meaning it is visible within procedures (see Shared).
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Jul 01, 2011 8:25

Basically, extern says 'this variable exists *somewhere*', and common says 'This variable exists *here*'.
Cjsheaf
Posts: 22
Joined: Jul 08, 2010 4:39

Postby Cjsheaf » Jul 04, 2011 22:17

I'm having another problem very similar (but not quite) to the previous issue. It's pretty much the same situation, except this time with a public function.

I'm trying to call a function to get a piece of private data from one of my managers, which is being called from another one of my managers. The compiler is just throwing more "Variable not declared" errors whenever I try to use a manager function from within another manager.

Again, I made absolutely sure that the manager file which contains the function is being #Included and an object which uses the type is being Defined well before the manager file trying to call the function is even #Included or Declared.

Is there a way I can apply the Common or Extern keywords to this situation? So far I'm at a loss.
Cjsheaf
Posts: 22
Joined: Jul 08, 2010 4:39

Postby Cjsheaf » Jul 07, 2011 19:53

Is there at least a way to indirectly use the function? My project is stalled until I can get this issue resolved. Unless I want to rewrite 90% of it that is. Every other language I've used allows this sort of inter-file communication.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Postby MichaelW » Jul 07, 2011 22:07

I don’t know about the OOP stuff, but for a normal function defined in another module you would need to include a matching function declaration in the calling module.
Cjsheaf
Posts: 22
Joined: Jul 08, 2010 4:39

Postby Cjsheaf » Jul 08, 2011 20:24

You do seem to be correct about a normal function, but the OOP apparently does throw a wrench into that solution for me, unless I'm getting the syntax wrong (entirely possible). I suppose I could simply expose the data I need to access, but then that defeats the purpose of encapsulating it.

I might just have to go with a single, long, wall-o-text like I used to for my old programs.
Dr_D
Posts: 2389
Joined: May 27, 2005 4:59
Contact:

Postby Dr_D » Jul 08, 2011 22:29

Can you make a small program that demonstrates what you're trying to accomplish? If you're trying to call something that the compiler possibly hasn't parsed yet, you may have to declare an arbitrary type for forward referencing.
Cjsheaf
Posts: 22
Joined: Jul 08, 2010 4:39

Postby Cjsheaf » Jul 09, 2011 1:08

Code: Select all

    '-----Main File-----
#Include "fbgfx.bi"
Using FB

#Include "Manager 1.bas"
    Dim Manager1 As Manager1_Type
#Include "Manager 2.bas"
    Dim Manager2 As Manager2_Type

Do
    Cls
   
    Print Manager1.GetData()
    Print Manager2.Add()
   
    Sleep 10, 1
Loop Until Multikey(SC_ESCAPE)


Code: Select all

    '-----Manager 1------

Type Manager1_Type
    Public:
        Declare Function GetData() As Integer
    Private:
        Dim As Integer Stored_Data = -1
End Type

Function Manager1_Type.GetData() As Integer
    Return Stored_Data
End Function


Code: Select all

    '-----Manager 2-----

Type Manager2_Type
    Public:
        Declare Function Add() As Integer
    Private:
        Dim As Integer AddBy = 1
End Type

Function Manager2_Type.Add() As Integer
    Return Manager1.Get_Data() + AddBy
End Function


That's the gist of what I'm trying to do distilled into its most basic form.

EDIT: Actually, now that I'm able to test the specific problem without much hassle, I get the same error even if I put the Manager2_Type in the Main File. In fact, it doesn't seem to work however I order it or wherever I place it. Now I'm very confused.
fxm
Posts: 9178
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Postby fxm » Jul 09, 2011 6:50

One solution, using 'Shared'

Manager 2:
Correct the syntax error:
Replace
Return Manager1.Get_Data() + AddBy
by
Return Manager1.GetData() + AddBy

Main file:
Replace
Dim Manager1 As Manager1_Type
by
Dim Shared Manager1 As Manager1_Type
Cjsheaf
Posts: 22
Joined: Jul 08, 2010 4:39

Postby Cjsheaf » Jul 09, 2011 17:59

Perfect! Typo aside, that's a really strange solution to my problem considering the initial post of this thread.

I guess I'll have to remember that Shared doesn't let regular variables be accessed by other project files, but for whatever reason, it lets UDTs be accessed.

That seems an awful lot like the counter-intuitive language quirks that always trip up coding newbies in the "harder" languages, and the lack of them is a big reason why I like coding in FreeBASIC.
Dr_D
Posts: 2389
Joined: May 27, 2005 4:59
Contact:

Postby Dr_D » Jul 09, 2011 18:10

Well, this will work, but it isn't really the recommended method. If you would be willing to try FBEdit, we could set this up as a project pretty easily. For instance, I have an include file for each module. So, you would have ModuleName.bas and ModuleName.bi, for each module, respectively. Therefore, you don't have any functions in your include files, only declarations. All of the executable code will be in the actual modules. You don't have to do it that way though, of course. ;)


Code: Select all

    '-----Main File-----
#Include "fbgfx.bi"
Using FB

#Include once "Manager1.bas"
#Include once "Manager2.bas"

Do
    Cls
   
    Print Manager1.GetData()
    Print Manager2.Add()
   
    Sleep 10, 1
Loop Until Multikey(SC_ESCAPE)



Code: Select all

    '-----Manager 1------

Type Manager1_Type
    Public:
        Declare Function GetData() As Integer
    Private:
        Dim As Integer Stored_Data = -1
End Type

Dim shared Manager1 As Manager1_Type

Function Manager1_Type.GetData() As Integer
    Return this.Stored_Data
End Function



Code: Select all

    '-----Manager 2-----

Type Manager2_Type
    Public:
        Declare Function Add() As Integer
    Private:
        Dim As Integer AddBy = 1
End Type

Dim shared Manager2 As Manager2_Type

Function Manager2_Type.Add() As Integer
    Return Manager1.GetData() + AddBy
End Function
Dr_D
Posts: 2389
Joined: May 27, 2005 4:59
Contact:

Postby Dr_D » Jul 09, 2011 18:45

I made a project for this, if you're interested...

http://jafile.com/uploads/dr_d/manager_example.zip

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 8 guests