GPI wrote:With StructureUnion you can create "Aliases"
StructureUnion with String and other Variable-Typ combined isn't a good idea (my opinion)).
Excellent help guys.. thanks.. as for this example, it came from the PB
Help File..
Ok, I've done some testing with everyones examples. Which is 99% of
how I grasp new things.
Code: Select all
Structure Type
Name$
StructureUnion
Long.l ; Each field (Long, Float and String) resides at the
Float.f ; place in memory.
String.s ;
EndStructureUnion
EndStructure
DefType.Type foo
foo\Name$ = "PB"
Debug foo\Name$
foo\String = "blah"
Debug foo\String ; the string
Debug @foo\String ; the address
Debug PeekS(@foo\String) ; pull string from address
foo\Float = 1.234
Debug foo\Float
foo\Long = 8765
Debug foo\Long
; So as you've stated, they are kind of like alias's. They can be
; utilized how ever you wish, as long as you have defined them
; first with StructureUnion.
;
; Is the main advantage of this, to save on memory(size)?
Here is the one that I was analyzing:
Code: Select all
Structure IPType
Reserved.w
Port.w
StructureUnion
IPLong.l
IP.b[4]
EndStructureUnion
Zeros.l[2]
EndStructure
length.l = SizeOf(IPType)
result.l = getpeername_(cnid, @IP.IPType, @length)
If result = 0
remoteip$ = StrU(IP\IP[0],#Byte)+"."+StrU(IP\IP[1], #Byte)+"."
;remoteip$ + StrU(IP\IP[2],#Byte)+"."+StrU(IP\IP[3], #Byte)+":"+StrU(IP\Port,#Word) ;remote port
remoteip$ + StrU(IP\IP[2],#Byte)+"."+StrU(IP\IP[3], #Byte); without remote port
Else
result = WSAGetLastError_()
EndIf
result.l = getsockname_(cnid, @IP.IPType, @length)
If result=0
localip$ = StrU(IP\IP[0],#Byte)+"."+StrU(IP\IP[1], #Byte)+"."
;localip$ + StrU(IP\IP[2],#Byte)+"."+StrU(IP\IP[3], #Byte)+":"+StrU(IP\Port,#Word) ;local port
localip$ + StrU(IP\IP[2],#Byte)+"."+StrU(IP\IP[3], #Byte) ; without local port
Else
result = WSAGetLastError_()
EndIf
Which will bring me to my next question:
In this line:
Code: Select all
result.l = getpeername_(cnid, @IP.IPType, @length)
Where he writes @IP.IPType is this where he is creating IP?
Instead of creating it first and then pointing to it like such?
IP.IPType or DefType.IPType IP
result.l = getpeername_(cnid, @IP.IPType, @length)
I just wasn't aware that you could create it and point to it in the same
command set. But its functional so I guess that's a yes.
-- and lastly, just to be sure that I understand StructureUnions
Code: Select all
Structure myNewType
a.s
b.s
c.s
StructureUnion
There.l
Can.w
Be.s
Only.b
One.f
EndStructureUnion
EndStructure
Sector1.myNewType
Sector1\a = "I love it when"
Debug Sector1\a
Sector1\b = "I learn new ways"
Debug Sector1\b
Sector1\c = "to handle data."
Debug Sector1\c
Debug ""
Sector1\There = 11
Debug Sector1\There
Sector1\Can = -30000
Debug Sector1\Can
Sector1\Be = "not recommended" ; @PB: could you tell me more/why not recommended?
Debug Sector1\Be
Sector1\Only = 127
Debug Sector1\Only
Sector1\One = 22.33
Debug Sector1\One
- np