View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
George Nicholson[_2_] George Nicholson[_2_] is offline
external usenet poster
 
Posts: 170
Default problems with my class

Below is some code from a very similar situation. How much of this approach
is appropriate for what you are trying to do is up to you.

I used an array variable to store my Ingredient information within a
Sandwich class (I also have a separate Ingredient class that contains
PrepTime, Inventory counters, etc. for a Restaurant simulator). Since any
given sandwich can have a variable number of ingredients, an Array seemed
the only way to create a dynamic ingredient list for each sandwich. I was
also storing # of Ingredient servings per sandwich, requiring a 2 dimension
array. Whether you need/want that complexity is up to you.

'****** Part of Piece/Sandwich class **********

Private mintIngredientCount As Integer
Private marrIngredients() As Variant 'IngrdID, Servings
....................

Public Property Get IngredientCount() As Integer
IngredientCount = mintIngredientCount
End Property

Private Property Let IngredientCount(ByVal iNewValue As Integer)
' Count is Incremented when Let_Ingredient is called
mintIngredientCount = iNewValue
If mintIngredientCount 0 Then
ReDim Preserve marrIngredients(1 To 2, 1 To mintIngredientCount)
End If
End Property

Public Property Get Ingredients(iDim1 As Integer, idim2 As Integer) As
Variant
Ingredients = marrIngredients(iDim1, idim2)
End Property

Public Property Let Ingredients(iDim1 As Integer, idim2 As Integer, ByVal
varNewValue As Variant)
'(1,x) = IngredientID (i.e., Name, ID#)
'(2,x) = IngredientQuant (i.e., # Servings)

If idim2 mintIngredientCount Then
' Expand array
Me.IngredientCount = idim2
End If

marrIngredients(iDim1, idim2) = varNewValue
End Property

******* end of class excerpt ******************

With oSandwich
.Name = "Testing Sandwich"
.Size = "Yeah"
.Ingredients(1,1) = "Tomatotes"
' ? Maybe .Ingredients(1,1) = rgSandwich.Offset(0, Index + 2).Value
.Ingredients(2,1) = 2 '# of servings

.Ingredients(1,2) = "Sauce"
.Ingredients(2,2) = 1

' Example of how to access the stored information.
For i = 1 to .IngredientCount
' List IngredientName, Servings
Debug.Print .Ingredients(1,i); ", "; Ingredients(2,i)
Next i
End With


HTH,
--
George Nicholson

Remove 'Junk' from return address.


"medicenpringles"
<medicenpringles.1x7kej_1129824331.2715@excelfor um-nospam.com wrote in
message news:medicenpringles.1x7kej_1129824331.2715@excelf orum-nospam.com...

i'm having a problem with one of my classes. i have 2 methods in my
class(among others):


Code:
--------------------
Property Let IngredientName(Index As Integer, sName As String)
If rgSandwich Is Nothing Then
IngredientName = ""
Else
rgSandwich.Offset(0, Index + 2).Value = sName
End If
End Property
Property Get IngredientName(Index As Integer) As String
If rgSandwich Is Nothing Then
UninitializedError
Else
IngredientName = rgSandwich.Offset(0, Index + 2).Value
End If
End Property
--------------------


and a macro in a module:


Code:
--------------------
Sub Testing()
Dim oSandwich As Sandwich
Dim oSandwiches As New Sandwiches

Set oSandwich = oSandwiches.Add("Testing Sandwich")

With oSandwich
.Name = "Testing Sandwich"
.Size = "Yeah"
' (see below)
End With

End Sub
--------------------


i want to be able to have something like .IngredientName(1) =
"Tomatoes" or .IngredientName(3) = "Sauce" where the "(see below)"
comment is, but i'm not sure on how to structure my properties in order
to do so. does anyone know how i can do this?

thanks ahead of time,
sven


--
medicenpringles


------------------------------------------------------------------------
medicenpringles's Profile:
http://www.excelforum.com/member.php...o&userid=16458
View this thread: http://www.excelforum.com/showthread...hreadid=477929