Namespace (again) This time for procedures

General FreeBASIC programming questions.
BasicScience
Posts: 475
Joined: Apr 18, 2008 4:09
Location: Los Angeles, CA
Contact:

Namespace (again) This time for procedures

Postby BasicScience » Nov 18, 2013 15:44

Why does this code work for variable x, but fail for the subroutine? Compiler gives error message:

undefined reference to `JUNK::PROCS::TEST1(float)@8

Code: Select all

Namespace Junk

    Type Procs
       dim as single x
       declare sub test1(byval z as single)
    End Type

END Namespace

dim as junk.procs MyProcs

MyProcs.x = 5
print MyProcs.x

MyProcs.Test1(10)

sleep

Sub Test1(byval z as single)
    print z
end sub
Muttonhead
Posts: 117
Joined: May 28, 2009 20:07

Re: Namespace (again) This time for procedures

Postby Muttonhead » Nov 18, 2013 16:41

Sub Test1 ist not defined as a member of the type procs.

Code: Select all

Namespace Junk

    Type Procs
       dim as single x
       declare sub test1(byval z as single)
    End Type
   
    Sub Procs.Test1(byval z as single)
        print z
    end sub

END Namespace

dim as junk.procs MyProcs

MyProcs.x = 5
print MyProcs.x

MyProcs.Test1(10)

sleep
BasicScience
Posts: 475
Joined: Apr 18, 2008 4:09
Location: Los Angeles, CA
Contact:

Re: Namespace (again) This time for procedures

Postby BasicScience » Nov 18, 2013 17:06

OK, I see your code works.

But, how should this be done if the goal is to create a BI file that uses Namespace to keep variable and procedure names specific, but the procedures are in a compiled library or are in other code modules? In these cases, the code for the procedure(s) cannot be included in the Namespace declaration.

Thanks.
BasicScience
Posts: 475
Joined: Apr 18, 2008 4:09
Location: Los Angeles, CA
Contact:

Re: Namespace (again) This time for procedures

Postby BasicScience » Nov 18, 2013 17:09

How about this...

It works, but is it using good practice?

Code: Select all

Namespace Junk

    Type Procs
       dim as single x
       declare sub test1(byval z as single)
    End Type

END Namespace

dim as junk.procs MyProcs

MyProcs.x = 5
print MyProcs.x

MyProcs.Test1(10)

sleep

Sub Junk.Procs.Test1(byval z as single)
    print z
end sub
fxm
Posts: 9178
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Namespace (again) This time for procedures

Postby fxm » Nov 18, 2013 17:27

Yes, OK.

Another variant, because we can have multiple namespace blocks with the same name:

Code: Select all

Namespace Junk

    Type Procs
       dim as single x
       declare sub test1(byval z as single)
    End Type

END Namespace

dim as junk.procs MyProcs

MyProcs.x = 5
print MyProcs.x

MyProcs.Test1(10)

sleep

Namespace Junk

    Sub Procs.Test1(byval z as single)
       print z
    end sub

END Namespace
BasicScience
Posts: 475
Joined: Apr 18, 2008 4:09
Location: Los Angeles, CA
Contact:

Re: (resolved) Namespace (again) This time for procedures

Postby BasicScience » Nov 18, 2013 18:21

I didn't realize a namespace could be split into multiple blocks. Got it now. Thanks

Return to “General”

Who is online

Users browsing this forum: No registered users and 6 guests