[SOLVED] If not Defined() then Define inside If/CompilerIf. Defined() always returns 0

Just starting out? Need help? Post your questions and find answers here.
destiny
User
User
Posts: 29
Joined: Wed Jul 15, 2015 12:58 pm
Location: CCCP

[SOLVED] If not Defined() then Define inside If/CompilerIf. Defined() always returns 0

Post by destiny »

Hi guys.
Can't make it work :(
I want to check if procedure/variable/constant/etc is defined or not and when not defined then include according file(s).
There is no difference if I use XIncludeFile/IncludeFile or replace it by Define MyParam.MyStruct/Global MyParam.MyStruct.
And which metod is better to use - "CompilerIf" or just "If" in that situation?

Test.pbi:

Code: Select all

Structure MyStruct
  Param.s
EndStructure

CompilerIf Defined(MyParam, #PB_Structure)
  Debug "1. Constant 'MyParam' is declared"
CompilerEndIf

CompilerIf Not Defined(MyParam, #PB_Structure)
  Debug "2. Constant 'MyParam' is not declared"
  IncludeFile "PathToFile\Struct.pbi"
CompilerEndIf

CompilerIf Defined(MyParam, #PB_Structure)
  Debug "3. Constant 'MyParam' is declared"
CompilerEndIf

If Defined(MyParam, #PB_Structure)
  Debug "4. Constant 'MyParam' is declared"
EndIf

If Not Defined(MyParam, #PB_Structure)
  Debug "5. Constant 'MyParam' is not declared"
  IncludeFile "PathToFile\Struct.pbi"
EndIf

If Defined(MyParam, #PB_Structure)
  Debug "6. Constant 'MyParam' is declared"
EndIf
PathToFile\Struct.pbi:

Code: Select all

Define MyParam.MyStruct
Last edited by destiny on Thu Feb 16, 2023 12:11 am, edited 1 time in total.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: If not Defined() then Define inside If/CompilerIf. Defined() always returns 0

Post by netmaestro »

Code: Select all

Structure MyStruct
  Param.s
EndStructure

CompilerIf Defined(MyStruct, #PB_Structure) <> 0
  Debug "1. Structure 'MyStruct' is declared"
CompilerEndIf

'MyParam' is no part of the structure. It's an error. (too much coffee?)
Last edited by netmaestro on Wed Feb 15, 2023 9:39 pm, edited 1 time in total.
BERESHEIT
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: If not Defined() then Define inside If/CompilerIf. Defined() always returns 0

Post by Paul »

Shouldn't it be...

Code: Select all

Structure MyStruct
  Param.s
EndStructure

CompilerIf Defined(MyStruct, #PB_Structure)
  Debug "1. Constant 'MyStruct' is declared"
CompilerEndIf

CompilerIf Not Defined(MyStruct, #PB_Structure)
  Debug "2. Constant 'MyStruct' is not declared"
CompilerEndIf

Code: Select all

;Structure MyStruct
;  Param.s
;EndStructure

CompilerIf Defined(MyStruct, #PB_Structure)
  Debug "1. Constant 'MyStruct' is declared"
CompilerEndIf

CompilerIf Not Defined(MyStruct, #PB_Structure)
  Debug "2. Constant 'MyStruct' is not declared"
CompilerEndIf

EDIT: netmaestro you are to quick !! :lol:
Image Image
destiny
User
User
Posts: 29
Joined: Wed Jul 15, 2015 12:58 pm
Location: CCCP

Re: If not Defined() then Define inside If/CompilerIf. Defined() always returns 0

Post by destiny »

Ok, maybe I asked wrong.
How can I understand that MyParam is Defined? Not MyStruct.
After Define MyParam.MyStruct Defined() is always answers False.
TypeOf() MyParam and MyStruct tells that both of them are Structure. (03-1, 03-2, 04).

And question about If or CompilerIf is still bothering me. Why CompilerIf and not If? :( It's because there will be less bytes in final executable because compiler will ignore that parts of code where Defined() will answer True but If ... True will be compiled always?

Code: Select all

Structure MyStruct
  Param.s
EndStructure

CompilerIf Defined(MyParam, #PB_Structure)
  Debug "01-1. Structure 'MyParam' is declared"
CompilerEndIf
CompilerIf Defined(MyStruct, #PB_Structure)
  Debug "01-2. Structure 'MyStruct' is declared"
CompilerEndIf

CompilerIf Not Defined(MyParam, #PB_Structure)
  Debug "02-1. Structure 'MyParam' is not declared"
  Define MyParam.MyStruct
CompilerEndIf
CompilerIf Not Defined(MyStruct, #PB_Structure)
  Debug "02-2. Structure 'MyStruct' is not declared"
  Define MyParam.MyStruct
CompilerEndIf

Debug "03-1. " + TypeOf(MyParam)
Debug "03-2. " + TypeOf(MyStruct)
Debug "04. " + #PB_Structure

CompilerIf Defined(MyParam, #PB_Structure)
  Debug "05-1. Structure 'MyParam' is NOW declared"
CompilerEndIf
CompilerIf Defined(MyStruct, #PB_Structure)
  Debug "05-2. Structure 'MyStruct' is NOW declared"
CompilerEndIf

CompilerIf Not Defined(MyParam, #PB_Structure)
  Debug "06-1. Structure 'MyParam' is STILL NOT declared"
CompilerEndIf
CompilerIf Not Defined(MyStruct, #PB_Structure)
  Debug "06-2. Structure 'MyStruct' is STILL NOT declared"
CompilerEndIf

If Defined(MyParam, #PB_Structure)
  Debug "11-1. Structure 'MyParam' is declared"
EndIf
If Defined(MyStruct, #PB_Structure)
  Debug "11-2. Structure 'MyStruct' is declared"
EndIf

If Not Defined(MyParam, #PB_Structure)
  Debug "12-1. Structure 'MyParam' is not declared"
  Define MyParam.MyStruct
EndIf
If Not Defined(MyStruct, #PB_Structure)
  Debug "12-2. Structure 'MyStruct' is not declared"
  Define MyParam.MyStruct
EndIf

If Defined(MyParam, #PB_Structure)
  Debug "13-1. Structure 'MyParam' is NOW declared"
EndIf
If Defined(MyStruct, #PB_Structure)
  Debug "13-2. Structure 'MyStruct' is NOW declared"
EndIf

If Not Defined(MyParam, #PB_Structure)
  Debug "14-1. Structure 'MyParam' is STILL NOT declared"
EndIf
If Not Defined(MyStruct, #PB_Structure)
  Debug "14-2. Structure 'MyStruct' is STILL NOT declared"
EndIf
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: If not Defined() then Define inside If/CompilerIf. Defined() always returns 0

Post by Demivec »

destiny wrote: Wed Feb 15, 2023 11:42 pm Ok, maybe I asked wrong.
How can I understand that MyParam is Defined? Not MyStruct.
After Define MyParam.MyStruct Defined() is always answers False.
TypeOf() MyParam and MyStruct tells that both of them are Structure. (03-1, 03-2, 04).
You define MyParam as a variable, not a structure.
Use

Code: Select all

CompilerIf Not Defined(MyParam, #PB_VARIABLE)
  Debug "Variable 'MyParam' is not declared"
  Define MyParam.MyStruct
CompilerEndIf
destiny
User
User
Posts: 29
Joined: Wed Jul 15, 2015 12:58 pm
Location: CCCP

Re: If not Defined() then Define inside If/CompilerIf. Defined() always returns 0

Post by destiny »

Demivec wrote: Wed Feb 15, 2023 11:51 pmYou define MyParam as a variable, not a structure.
Yep, found it when tested:

Code: Select all

Debug "21-01. " + Defined(MyParam, #PB_Constant)
Debug "21-02. " + Defined(MyParam, #PB_Variable)
Debug "21-03. " + Defined(MyParam, #PB_Array)
Debug "21-04. " + Defined(MyParam, #PB_Structure)
Debug "21-05. " + Defined(MyParam, #PB_List)
Debug "21-06. " + Defined(MyParam, #PB_Map)
Debug "21-07. " + Defined(MyParam, #PB_Interface)
Debug "21-08. " + Defined(MyParam, #PB_Procedure)
Debug "21-09. " + Defined(MyParam, #PB_Function)
Debug "21-10. " + Defined(MyParam, #PB_OSFunction)
Debug "21-11. " + Defined(MyParam, #PB_Label)
Debug "21-12. " + Defined(MyParam, #PB_Prototype)
Debug "21-13. " + Defined(MyParam, #PB_Module)
Debug "21-14. " + Defined(MyParam, #PB_Enumeration)
21-02 result was True.
Require a lot of rest. :|
Thanx to you all.
Post Reply