Can default for a Structure be a null?
Can default for a Structure be a null?
Is this a valid procedure declaration in Purebasic 4.50?
Procedure FindRecords(db, recstructure = #NULL)
...
EndProcedure
Where db is a database handle and recstructure is a structure.
My question is actually "Can the default value for a structure be a NULL?"
Procedure FindRecords(db, recstructure = #NULL)
...
EndProcedure
Where db is a database handle and recstructure is a structure.
My question is actually "Can the default value for a structure be a NULL?"
Re: Can default for a Structure be a null?
#NULL is just a constant with the value 0. The procedure is perfectly valid, if you pass only one parameter when calling the procedure, recstructure will be 0 inside the procedure.
The only problem with your question is that you mention "structure", but recstructure is not a structure, it's a normal integer.
The only problem with your question is that you mention "structure", but recstructure is not a structure, it's a normal integer.
Re: Can default for a Structure be a null?
Thank you Trond,
I fear I'm not using the proper method. I want to be able to pass a structure to the procedure, but need to handle the case where no values have been defined within the structure.
Would this work?
Procedure FindRec(db,RecStructure)
If RecStructure
....
Else
....
Endif
or this?
Procedure FindRec(db, RecStructure)
If RecStructure = ClearStructure(RecStructure,StructureDefinition)
....
Else
....
Endif
I worry that the last method might actually clear the structure, but maybe the use of a "dummy" structured variable would work
Procedure FindRec(db, RecStructure)
If RecStructure = ClearStructure(DummyStructure,StructureDefinition)
....
Else
....
Endif
Any thoughts?
I fear I'm not using the proper method. I want to be able to pass a structure to the procedure, but need to handle the case where no values have been defined within the structure.
Would this work?
Procedure FindRec(db,RecStructure)
If RecStructure
....
Else
....
Endif
or this?
Procedure FindRec(db, RecStructure)
If RecStructure = ClearStructure(RecStructure,StructureDefinition)
....
Else
....
Endif
I worry that the last method might actually clear the structure, but maybe the use of a "dummy" structured variable would work
Procedure FindRec(db, RecStructure)
If RecStructure = ClearStructure(DummyStructure,StructureDefinition)
....
Else
....
Endif
Any thoughts?
Re: Can default for a Structure be a null?
Values are always defined. They are initialized to 0 automatically when a variable is declared or when you allocate memory.I want to be able to pass a structure to the procedure, but need to handle the case where no values have been defined within the structure.
None of your procedures accept a structure as the parameter, so I still don't understand what your problem is.
Re: Can default for a Structure be a null?
Using only the limited information you've provided:WoodLark wrote:I fear I'm not using the proper method. I want to be able to pass a structure to the procedure, but need to handle the case where no values have been defined within the structure.
Code: Select all
Structure someStructure
;....
EndStructure
Procedure FindRec(dB,*RecStructure.someStructure = #Null)
;test the value of the pointer
If *RecStructure
;an address to a structure was supplied
;....
Else
;a #Null value for structure
;....
EndIf
;....
EndProcedure
Define dB, Rec.someStructure
;pass a structure
FindRec(dB, Rec) ;or the equivalent FindRec(dB, @Rec)
;don't pass a structure
FindRec(dB) ;uses default of #Null for structure
Re: Can default for a Structure be a null?
The code that Demivec posted will work if you don't pass a structure to the procedure. But I think what you need to do is check each item in the structure, in the procedure, to see if they are 0 or "" or whatever.
You can create an empty version and compare the data pretty quckly. A modified version of Demivec's code:
Also, To answer your original question:
The "default value" of a Structure is the address of where that Structure resides in memory, so even if it is Empty, it will have a value Unless it doesn't even exist. If you declare a Structure, it will exist even if it is Empty.
Hope this helps.
You can create an empty version and compare the data pretty quckly. A modified version of Demivec's code:
Code: Select all
Structure someStructure
a.f
b.i
c.s
EndStructure
Procedure FindRec(dB,*RecStructure.someStructure = #Null)
fake.someStructure
;test the value of the pointer
If *RecStructure
;an address to a structure was supplied
;see if it is empty:
If PeekS(@fake, SizeOf(someStructure)) <> PeekS(*RecStructure, SizeOf(someStructure))
;it is not empty, you may continue.
Debug "Not empty"
;....
Else
Debug "Empty"
;It is empty
EndIf
Else
;a #Null value for structure
Debug "Is NULL"
;....
EndIf
;....
EndProcedure
Define dB, Rec.someStructure
;pass a structure
FindRec(dB, Rec) ;or the equivalent FindRec(dB, @Rec)
;don't pass a structure
FindRec(dB) ;uses default of #Null for structure
;now we'll fill in some of the structure
Rec\a = 1.05
Rec\b = 20
Rec\c = "Hi"
FindRec(dB, Rec)
My question is actually "Can the default value for a structure be a NULL?"
The "default value" of a Structure is the address of where that Structure resides in memory, so even if it is Empty, it will have a value Unless it doesn't even exist. If you declare a Structure, it will exist even if it is Empty.
Hope this helps.
Last edited by GBeebe on Mon Jun 14, 2010 2:17 am, edited 2 times in total.
- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
Re: Can default for a Structure be a null?
I think there is an understanding problem:
Structures are only passed by Reference, never by Value.
so, when you want to pass a stuctured Value, you need to pass a pointer to a Stucture outside the Procedure.
the stuctured Variable can be empty, e.g. every field = 0,
but the Value passed to the Procedure will be a valid pointer to the empty Structure.
(or like Demivec showed, replaced by a #NULL pointer to indicate the Rect outside wasn't assigned)
...
when you want to pass a Structure byVal to a Procedure, you'll need to build a container first, and it will be limited to 64bit of size.
you can pass a Rectangle byVal, when you pass it as a Quad containing four Word-Size Coordinates.
Structures are only passed by Reference, never by Value.
so, when you want to pass a stuctured Value, you need to pass a pointer to a Stucture outside the Procedure.
the stuctured Variable can be empty, e.g. every field = 0,
but the Value passed to the Procedure will be a valid pointer to the empty Structure.
(or like Demivec showed, replaced by a #NULL pointer to indicate the Rect outside wasn't assigned)
...
when you want to pass a Structure byVal to a Procedure, you'll need to build a container first, and it will be limited to 64bit of size.
you can pass a Rectangle byVal, when you pass it as a Quad containing four Word-Size Coordinates.
oh... and have a nice day.
Re: Can default for a Structure be a null?
I am not making myself clear; I apologize. Here (I hope) is a better example of what I am trying to accomplish:
Structure ARecord
field1.q
field2.q
field3.s
EndStructure
Procedure FindRec(db, MyRecord.ARecord )
If MyRecord
;Do something
Else ; none of the variables in the structure have been assigned any values other than their defaults (as if a ClearStructure() had been performed just before the call).
;Do something different
Endif
EndProcedure
Does this make any more sense?
Structure ARecord
field1.q
field2.q
field3.s
EndStructure
Procedure FindRec(db, MyRecord.ARecord )
If MyRecord
;Do something
Else ; none of the variables in the structure have been assigned any values other than their defaults (as if a ClearStructure() had been performed just before the call).
;Do something different
Endif
EndProcedure
Does this make any more sense?
Re: Can default for a Structure be a null?
Reading it again I stand firm that my my previous reply is what you're looking for.
Here, I modified it a bit to match your ARecord structure:
Here, I modified it a bit to match your ARecord structure:
Code: Select all
Structure ARecord
field1.q
field2.q
field3.s
EndStructure
Procedure FindRec(dB, *MyRecord.ARecord = #Null)
fake.ARecord
;test the value of the pointer
If *MyRecord
;an address to a structure was supplied
;see if it is empty:
If PeekS(@fake, SizeOf(ARecord)) <> PeekS(*MyRecord, SizeOf(ARecord))
;it is not empty, you may continue.
Debug "Here MyRecord IS NOT Empty"
;Do something
Else
Debug "Here MyRecord IS Empty"
;Do something different
EndIf
Else
;for some reason, you may never need it, but here MyRecord doesn't exist.
Debug "Here MyRecord IS NULL"
;....
EndIf
;....
EndProcedure
Define dB, Rec.ARecord
;pass a structure
;This is an Empty Record.
FindRec(dB, Rec) ;or the equivalent FindRec(dB, @Rec)
;don't pass a structure
;No record is passed, which will may *MyRecord = #Null or zero.
FindRec(dB) ;uses default of #Null for structure
;now we'll fill in some of the structure so it IS NOT EMPTY.
Rec\field1 = 10
Rec\field2 = 555
Rec\field3 = "Something"
FindRec(dB, Rec)
Re: Can default for a Structure be a null?
Thank you GBeebe,
I think I understand now. I'll give it a try.
I think I understand now. I'll give it a try.
Re: Can default for a Structure be a null?
Not a problem, WoodLark. I'm still amazed that you've been a member since '05 and up until today, you've only had 6 posts.
Re: Can default for a Structure be a null?
@WoodLark: Here is a code sample with a slight variation in the way that an empty structure is tested for. It varies from GBeebe's in only one or two minor ways.
Code: Select all
Structure ARecord
field1.q
field2.q
field3.s
EndStructure
Procedure FindRec(dB, *MyRecord.ARecord) ;structures are passed by reference, i.e. a pointer
Static blankRecord.ARecord ;empty record
If *MyRecord
If CompareMemory(*MyRecord, @blankRecord, SizeOf(ARecord)) = 0
;Do something
Debug "Non-empty structure supplied."
Else ;all of the structure's values are the same as as a new structure
;Do something different
Debug "Empty record supplied."
EndIf
Else ;a value of #Null was supplied for the address of ARecord
;Do nothing or handle error
Debug "Error, record pointer is #Null."
EndIf
EndProcedure
Define dB, Rec.ARecord
;This is an Empty Record.
FindRec(dB, Rec) ;or the equivalent FindRec(dB, @Rec)
;No record is passed, i.e. #Null)
FindRec(dB, #Null)
;now we'll fill in some of the structure so it IS NOT EMPTY.
Rec\field1 = 10
Rec\field2 = 555
Rec\field3 = "Something"
FindRec(dB, Rec)
- Kaeru Gaman
- Addict
- Posts: 4826
- Joined: Sun Mar 19, 2006 1:57 pm
- Location: Germany
Re: Can default for a Structure be a null?
Structures cannot be passed by Value, only by Reference, means you need a pointer there.WoodLark wrote:Code: Select all
Procedure FindRec(db, MyRecord.ARecord )
oh... and have a nice day.
Re: Can default for a Structure be a null?
When I joined in '05, I was gainfully employed and was required to use vb.net. Since I retired, I've only written one small Purebasic app, but now I decided to take on a more ambitious project. I develop in Linux, but want to be able to compile cross-platform apps.GBeebe wrote:Not a problem, WoodLark. I'm still amazed that you've been a member since '05 and up until today, you've only had 6 posts.
Re: Can default for a Structure be a null?
Demivec's approach is probably the better way to go. It is basically the same thing though. Also, I only program in Linux.