Posts: 3,616
Threads: 287
Joined: Jan 2003
Explaining what Types are to cruz sparked this question in my mind (actually, I came up with this problem in C++):
Say you have a Type that acts as a template for the a database of different sales of houses you make as a real estate agent. The template will look like this:
Code: Type saleTemplate
numOfSales As Integer
addressOfSale(numOfSales) As String * 100
End Type
The second spec in the template is an array, that contains the amount of elements that numOfSales has. Ahhh! That won't work! Because when you Dim your variable as type saleTemplate, it will reserve memory for all the parts of the template. And with the following code:
Code: Type saleTemplate
numOfSales As Integer
addressOfSale(numOfSales) As String * 100
End Type
Dim Agent1Sales As saleTemplate
The last statement reserves memory for everything declared in saleTemplate. But the compiler doesn't know how what the value of numOfSales is yet (well, actually, it's zero, but that doesn't help us), so how can it put away memory for an array who-knows-how-big?
I'm pretty sure that we need an equivalent of what a class-constructer is in OOP.
Is that even possible?
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Posts: 480
Threads: 24
Joined: Mar 2003
I'm not sure why you would include NumOfSales inside the TYPE structure. Why not do:
as a stand-alone integer variable, and
Code: TYPE SalesTemplate
AddressOfSale AS STRING * 100
NameOfSalesPerson AS STRING * 100
.
. . . etc . . .
.
CostOfHouse AS SINGLE
END TYPE
as your Record, then:
Code: DIM SHARED ArrayOfSales(1 to NumOfSales%) AS SalesTemplate
That outta work alright.
Posts: 3,616
Threads: 287
Joined: Jan 2003
Yes, yes. You could. It would be better, in fact.
But I ask this question also for theory. And application! For instance, in C++, if you want to use string.h, you need to make your strings arrays of single characters. So, when I was writing a struct (a C++ TYPE) to model my strings:
Code: struct stringModel
{
int numChars;
char stringAr[numChars];
}
That won't work. Well, first of all, to declare arrays without a literal, you need a constant. And second, it doesn't know how much space to allocate.
So, *in theory*, is it possible (in QB/VB) to do what I want?
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Posts: 3,368
Threads: 195
Joined: Jan 2003
OOoohh....... oohhhhhh........
Well, good question.
Answer is, QB is stupid.
--------------------------------------
You can do it another way: you just have to think "Aga's Way"... Meg's solution takes up much more space than it should...
So, for each agent, you have a numOfSales and an addressOfSale array. OK.
Code: numOfSalesTot = 100
agentAmount = 10
DIM agent.numOfSales(1 to agentAmount) AS INTEGER
DIM agent.addressOfSale(1 TO numOfSalesTot) AS STRING * 100
DIM agent.endAddress(0 TO agentAmount) AS INTEGER
Now, you get the addresses of each sale by taking the last agent.endAddress() plus one, and the current agent.endAddress, and running through them:
Code: FOR i% = agent.endAddress(currentAgent - 1) + 1 TO agent.endAddress(currentAgent)
PRINT agent.addressOfSale(1 TO numOfSalesTot)
NEXT i%
Now, unlike Meg's solution, this doesn't lend itself very well to actual salesperson length changes, but it takes up a LOT less room to store. To fix this, you could add another array to specify the agent's NUMBER and get rid of "DIM agent.endAddress(0 TO agentAmount) AS INTEGER"..... which is almost like Meg's solution..
Or you could do other things......
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 480
Threads: 24
Joined: Mar 2003
I think I see what you're asking, Zack. There's no way to do that with the TYPE statement, as all TYPES are fixed-length (to my knowledge). You could just make a bunch of arrays, tho, instead.
*peace*
Meg.
Posts: 3,616
Threads: 287
Joined: Jan 2003
Aga: Yes, yes...I not that stingy about memory (besides, what difference will it make? A couple of kilobytes?). I just wanted the theory.
And Meg, thanks. I guess we need an object-oriented language for this, and we can make a class with a constructor that might be able to init it. But I can't discuss that yet, I'm too new to those concepts.
Thanks all.
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Posts: 691
Threads: 5
Joined: Apr 2002
Quote:So, when I was writing a struct (a C++ TYPE) to model my strings:
Code: struct stringModel
{
int numChars;
char stringAr[numChars];
}
That won't work. Well, first of all, to declare arrays without a literal, you need a constant. And second, it doesn't know how much space to allocate.
You can do what you want like this in C/C++:
Code: typedef struct {
int numStrings;
char **stringArray;
}
The stringArray variable is a dynamic two dimensional array, it can contain any number of strings, each of which can be any arbitary length. You will need to use malloc/realloc to allocate space for the array and the strings.
IIRC, QB 7.1 supports arrays in types, but I dont think it allows dynamic arrays in types. You could try my memory routines (available at QBNZ) which add simple dynamic memory to QB, you may be able to use those to achieve what you want.
esus saves.... Passes to Moses, shoots, he scores!
Posts: 3,616
Threads: 287
Joined: Jan 2003
I could fool around with memory, yes...but that's apt to be incompatible with some things.
Anyway, no matter. I never go as far as two-dimensional arrays in C++.
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
|