Array in Type

BlitzMax Forums/BlitzMax Programming/Array in Type

Vertex(Posted 2005) [#1]
Hi!
Following problem:
Type TWSAData
   Field shVersion:Short 
   Field shHighVersion:Short 
   Field bDescription:Byte[256+1] 
   Field bSystemStatus:Byte[128+1] 
   Field shMaxSockets:Short 
   Field shMaxUdpDg:Short 
   Field pbVendorInfo:Byte Ptr 
End Type


will be convert by BMax to
Type TWSAData
   Field shVersion:Short 
   Field shHighVersion:Short 
   Field Pointer to bDescription[] 
   Field Pointer to bSystemStatus[]
   Field shMaxSockets:Short 
   Field shMaxUdpDg:Short 
   Field pbVendorInfo:Byte Ptr 
End Type


But i need the arrays bDescription and bSystemStatus IN THE Type and not outside with linked per pointer. Like this

Type TWSAData
   Field shVersion:Short 
   Field shHighVersion:Short 
   Field bDescription[0]:Byte
   Field bDescription[1]:Byte
   Field bDescription[2]:Byte
   Field bDescription[3]:Byte
   ...
   Field bDescription[256]:Byte
   Field bSystemStatus[0]:Byte
   Field bSystemStatus[1]:Byte
   Field bSystemStatus[2]:Byte
   Field bSystemStatus[3]:Byte
   ...
   Field bSystemStatus[128]:Byte
   Field shMaxSockets:Short 
   Field shMaxUdpDg:Short 
   Field pbVendorInfo:Byte Ptr 
End Type


cu olli


Perturbatio(Posted 2005) [#2]
Type test
Field ar:Int[10]
End Type

Local t:test = New test

For Local a = 0 To Len(t.ar)-1
t.ar[a] = Rand(1,23)
Next

For Local b = EachIn t.ar
Print b
Next


ImaginaryHuman(Posted 2005) [#3]
Or you could add a New() method to your type which will get run whenever you create a new instance of the type. ... the New() method can then initialize the array however you want it.

Your type can say Field bDescription:Byte[] and your Method can say bDescription=New Byte[20] or however big you want it, and initialize it with data.


Vertex(Posted 2005) [#4]
No, New assign only a address to a pointer, not more.
Take a look to this please:
Const SOCKET_ERROR = -1

Extern "Os"
	Function s_WSAStartup:Int(shVersion:Short, tWSA:Byte Ptr) = "WSAStartup@8" 
	Function s_WSAGetLastError:Int() = "WSAGetLastError@0" 
	Function s_WSACleanup:Int() = "WSACleanup@0"

	Function MemCopy2(dest:Byte Ptr, src:Int, size:Int) = "bbMemCopy"
End Extern

Function MAKESHORT:Short(bA:Byte, bB:Byte)
	Return bA | (bB Shl 8) 
End Function

Local tWSA:TBank
Local iIndex:Int
Local bChar
Local sDescription:String
Local sSystemStatus:String
Local pVendorinfo:Byte Ptr

tWSA = CreateBank(400)
Print "Starting Winsock..."
If s_WSAStartup(MAKESHORT(2, 0), BankBuf(tWSA)) = SOCKET_ERROR Then
	Print "Error: "+s_WSAGetLastError()
Else
	Print "OK"
EndIf


For iIndex = 4 To 260
	bChar = PeekByte(tWSA, iIndex)
	If bChar = 0 Then Exit
	sDescription = sDescription+Chr(bChar)
Next

For iIndex = 261 To 393
	bChar = PeekByte(tWSA, iIndex)
	If bChar = 0 Then Exit
	sSystemStatus = sSystemStatus+Chr(bChar)
Next

Print "Version:      "+PeekShort(tWSA, 0)
Print "HighVersion:  "+PeekShort(tWSA, 2)
Print "Description:  "+sDescription
Print "SystemStatus: "+sSystemStatus
Print "MaxSockets:   "+Peekshort(tWSA, 394)
Print "MaxUdpDg:     "+PeekShort(tWSA, 398)

Print "Closing Einsock..."
If s_WSACleanup() = SOCKET_ERROR Then
	Print "Error: "+s_WSAGetLastError()
Else
	Print "OK"
EndIf

Release tWSA ; FlushMem
Delay 2000
End


You can see it, that the structure don't save a pointer, it save the content of the array. The next funcking thing is, that I must create a new instance of a type to use it. It isn't sufficient to use only a pointer and giveup it to the extern function that will be fill the type with any values.

cu olli


Vertex(Posted 2005) [#5]
Hmm ok i have made it so: http://blitzbasic.com/codearcs/codearcs.php?code=1270
(s_WSAStartup / TWSAData)
by using Banks

cu olli