Qbasicnews.com
July 05, 2020, 03:20:51 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Arrays in Types  (Read 3186 times)
Torahteen
Ancient Guru
****
Posts: 744



« on: December 28, 2005, 02:22:22 AM »

Is this not possible? I've had a few small projects that tried and failed at this. What's goin' on?
Logged

quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
SSC
Guru
**
Posts: 288



WWW
« Reply #1 on: December 28, 2005, 02:32:03 AM »

I recently posted an example in freebasic.net

here is what I am guessing you want to do
Code:
Type mytype
variable(30) as integer
. . .
End Type

which you cannot do, however you can do it like this:
Code:
Type mytype
variable as integer
. . .
End Type

Dim array(1 to 30) as mytype

which will give you an array containing all of the variables in the type declaration as in array(1).variable or array(2).variable and so on
Logged

url=http://www.smithcosoft.com]SmithcoSoft Creations[/url]
"If you make it idiot proof, someone will make a better idiot" - Murphy's Law
TheBlueKeyboard
Wandering Guru
***
Posts: 334



« Reply #2 on: December 28, 2005, 09:39:43 AM »

Im not sure what you mean but it does work making an array inside a type.

Code:

Type mytype_t
    variable(30) As Integer
End Type

Dim myinst As mytype_t

myinst.variable(5) = 1337
Print myinst.variable(5)
Sleep
Logged

It's the difference between asking someone how much flour goes into pancakes, and handing them a sorry mix of oozing green goo and asking them to fix it." - Deleter

-Founder & President of the No More Religion Threads movement-
Anonymous
Guest
« Reply #3 on: December 28, 2005, 12:55:24 PM »

yes, dynamic arrays are what everyone wants

you use pointers like so

Code:
Type business

  owners As Integer
  owner_name As String Ptr
 
End Type

Declare Function alloc_owner_name( num As Integer = 0, s As String Ptr = 0 )


Dim As business b( 1 )

'' init
b( 0 ).owners = 1
b( 1 ).owners = 3


'' set up
With b( 0 )

  .owner_name = alloc_owner_name( .owners )
 
End With
With b( 1 )

  .owner_name = alloc_owner_name( .owners )
 
End With


'' 1 owner
b(0).owner_name[0] = "Bill Gates"

'' 3 owner
b(1).owner_name[0] = "Completely"
b(1).owner_name[1] = "Random"
b(1).owner_name[2] = "People"


For c = 0 To 1
  ? "business " & c + 1
  ?
  With b( c )
 
  For ct = 0 To .owners - 1
    ? "owner #" & ct + 1
    ? .owner_name[ct]
    .owner_name[ct] = "" ''clean!!

  Next
 
  ?
 
 
  End With

Next

Sleep

b( 0 ).owner_name = alloc_owner_name( 0 ) ''give it nothing!
b( 1 ).owner_name = alloc_owner_name



Function alloc_owner_name( num As Integer = 0, s As String Ptr = 0 )

  Deallocate s

  Return CAllocate( Len( String ) * num )
 
End Function


always clean up when you make a mess
Logged
Kylemc
Guru
**
Posts: 291



WWW
« Reply #4 on: December 28, 2005, 01:57:10 PM »

Quote from: "SSC"

which you cannot do,


Um..you can? TBK's example works for me on 0.15.
Logged
Torahteen
Ancient Guru
****
Posts: 744



« Reply #5 on: December 28, 2005, 02:00:39 PM »

Ok, in my program, I have a type that has an array inside it.

Code:
Type SomeType
array(1 To 12) As Byte
End Type


Then I have an array of SomeTypes.

Code:
Dim Shared SomeArray(1 To 10) As SomeType


But it doesn't appear to work.
Logged

quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
speedlemon
I hold this place together
*****
Posts: 874



« Reply #6 on: December 28, 2005, 02:10:11 PM »

It appears to work for me. :wink:
Logged
yetifoot
Ancient Guru
****
Posts: 575



« Reply #7 on: December 28, 2005, 02:10:34 PM »

This works fine for me in 0.15

Code:
Type SomeType
array(1 To 12) As Byte
End Type

Dim Shared SomeArray(1 To 10) As SomeType

SomeArray(1).Array(1) = 50
SomeArray(10).Array(11) = 100
Print SomeArray(1).Array(1)
Print SomeArray(10).Array(11)
Sleep
Logged

EVEN MEN OF STEEL RUST.
SSC
Guru
**
Posts: 288



WWW
« Reply #8 on: December 28, 2005, 06:42:00 PM »

hmmm, well I know you couldn't do it in QB,  but I am too lazy to bust out my laptop to test it right now so I guess i'll take your word for it...
Logged

url=http://www.smithcosoft.com]SmithcoSoft Creations[/url]
"If you make it idiot proof, someone will make a better idiot" - Murphy's Law
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!