Hi, I'm fairly new to VB, so I'm still not sure of what can or can't be done. I'm creating a Class called Element, which will contain all the data for a single element (believe it or not).
The number of properties required is so large (probably a few hundred) that I was wondering if i could combine some of them.
For example, if I had temperatures 1 to 9, i could store them in the array Temperature(8). But can I create a single property to access that entire array, or do I absolutely need a property for each value. I thought of using functions to access the temperatures, along the lines of:
Public Sub SetTemp(ByVal Value as Single, ByVal WhichOne as Integer)
Temperature(WhichOne) = Value
End Sub
but then I need a SetTemp function and a GetTemp function, and is obviously not ideal. Any ideas?
Comments
: The number of properties required is so large (probably a few hundred) that I was wondering if i could combine some of them.
:
: For example, if I had temperatures 1 to 9, i could store them in the array Temperature(8). But can I create a single property to access that entire array, or do I absolutely need a property for each value. I thought of using functions to access the temperatures, along the lines of:
:
: Public Sub SetTemp(ByVal Value as Single, ByVal WhichOne as Integer)
: Temperature(WhichOne) = Value
: End Sub
:
: but then I need a SetTemp function and a GetTemp function, and is obviously not ideal. Any ideas?
AFAIK you can return anything from a property method, including arrays.
[size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
[code]
$ select * from users where clue > 0
no rows returned
[/code]
: : The number of properties required is so large (probably a few hundred) that I was wondering if i could combine some of them.
: :
: : For example, if I had temperatures 1 to 9, i could store them in the array Temperature(8). But can I create a single property to access that entire array, or do I absolutely need a property for each value. I thought of using functions to access the temperatures, along the lines of:
: :
: : Public Sub SetTemp(ByVal Value as Single, ByVal WhichOne as Integer)
: : Temperature(WhichOne) = Value
: : End Sub
: :
: : but then I need a SetTemp function and a GetTemp function, and is obviously not ideal. Any ideas?
:
: AFAIK you can return anything from a property method, including arrays.
:
:
: [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
:
: [code]
: $ select * from users where clue > 0
: no rows returned
: [/code]
:
:
You can create Array properties, but they either have to return the Variant data-type or you have to delegate to an internal array like so:
[code]
private mTemps(9) As Single
Public Property Get Temperature(ByVal index As Integer) As Single
Temperature = mTemps(index)
End Property
Public Property Let Temperature(ByVal index As Integer, value As Single)
mTemps(index) = value
End Property
[/code]
Then you can set this in code like so ...
[code]
Dim c As Class1
Set c = New Class1
c.Temperature(0) = 55.7
c.Temperature(1) = 77.3
[/code]
You can make Temperature the default property in the class, by going to "Tools->Procedure Attributes" menu and selecting Temperature from the drop down. Then you click "Advanced" and select "(Default)" from the procedure id drop down. Then you can write your code like this ...
[code]
Dim c As Class1
Set c = New Class1
c(0) = 55.7
c(1) = 77.3
[/code]
Of course there is more wok that would need to be done to your class. This includes adding range checking in the property procedures or writing internal routines that cause your internal array to grow when you need to store more data.
Otherwise you can use variant properties and make array assignments like so:
[code]
Private mTemps As Variant
Public Property Get Temperature() As Variant
Temperature = mTemp
End Property
Public Property Let Temperature(arr As Variant)
If VarType(arr) <> vbArray + vbSingle Then
Err.Raise 13, "Temperature", "Type Mismatch: Expecting A Single Array"
End If
mTemp = arr
End Property
[/code]
You could then assign and use the array property like so ...
[code]
Dim arr(1)
arr(0) = 55.7
arr(1) = 77.3
c.Temperature = arr
Dim v
For Each v In c.Temperature
Debug.Print v
Next
End Sub
[/code]
:
: Public Sub SetTemp(ByVal Value as Single, ByVal WhichOne as Integer)
: Temperature(WhichOne) = Value
: End Sub
:
: but then I need a SetTemp function and a GetTemp function, and is obviously not ideal. Any ideas?
Maybe I don't understand the problem but that seems like the best way to me. Create an array value in your class. The use Get or Set as you mentioned above. Then when you use you're class you'll access the temperature just like anything else.
[code]
Private m_intTemp(9) As Integer
Public Property Get Temp(intWhichOne As Integer) As Integer
Temp = m_intTemp(intWhichOne)
End Property
Public Property Set Temp(intWhichOne As Integer, intValue As Integer)
m_intTemp(intWhichOne) = intValue
End Property
[/code]
Then in you're program you access the Temp by
[code]
intTemperature = MyClass.Temp(0)
MyClass.Temp(1) = 7
[/code]
Another thing: Some built-in functions have a description of what a certain variable to be passed to the function represents (in the tooltip-type display). For example the "int" function describes what Number is meant to be. Is there a way to include something like that for my properties, so that I could specify that index must be between 1 and 9 for example?
: Another thing: Some built-in functions have a description of what a certain variable to be passed to the function represents (in the tooltip-type display). For example the "int" function describes what Number is meant to be. Is there a way to include something like that for my properties, so that I could specify that index must be between 1 and 9 for example?
:
Yes and no (I'm talking about VB6).
By that I mean that there is a window that lets you add descriptions to your functions / sub / properties (it is in the Tools->Procedure attributes menu). However this doesn't prevent incorrect values to be passed to functions.
A more efficient way is to raise an error:
[code]
Public property Get MyProp(ParamValue as integer)
if (ParamValue < 1) or (ParamValue > 9) then err.raise (et cetera)
' property body follows
[/code]
Doing so will effectively prevent everyone from using incorrect values, and raising an error whenever this happens. Coupled with a good description of the error, IMO it works wonders, though.