Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array as property problem 3

Status
Not open for further replies.

jasonp45

Programmer
Aug 23, 2001
212
0
0
US
I'm trying to use a string array as a property. I can read the LBound/UBound so it seems the array is loading properly during the 'Let', and the 'Get' is working for the boundary functions, but I can't seem to access the values. Any ideas?

Code:
Set oMyClass = New cArrayTest
With oMyClass
	.Files_List = "File 1"
	.Files_List = "File 2"
	.Files_List = "File 3"
	MsgBox "LBound: " & LBound(.Files_List) & vbcrlf & "UBound: " & UBound(.Files_List)
	MsgBox .Files_List(0)
End With
WScript.Quit

Class cArrayTest

  Private mFiles_List() 'as String [Array]
  Private mFiles_List_Counter 'as Integer
  
  Private Sub Class_Initialize()
	ReDim mFiles_List(0)
  End Sub

  Public Property Let Files_List(ByVal vData)
	mFiles_List_Counter = mFiles_List_Counter + 1
	ReDim Preserve mFiles_List(mFiles_List_Counter)
	mFiles_List(mFiles_List_Counter) = vData
  End Property
  
  Public Property Get Files_List()
	Files_List = mFiles_List
  End Property
  
End Class
 
I'm not sure how to get an array to work, but here's a solution using a dictionary object.
Code:
Set oMyClass = New cArrayTest
With oMyClass
    .Files_List = "File 1"
    .Files_List = "File 2"
    .Files_List = "File 3"
    MsgBox .Files_List.Count
    MsgBox .Files_List.Item(1)
End With
WScript.Quit

Class cArrayTest

  Private mFiles_List 'as String [Array]
  Private mFiles_List_Counter 'as Integer
  
  Private Sub Class_Initialize()
    Set mFiles_List = CreateObject("Scripting.Dictionary")
  End Sub

  Public Property Let Files_List(ByVal vData)
    mFiles_List_Counter = mFiles_List_Counter + 1
    mFiles_List.Add mFiles_List_Counter, vData
  End Property
  
  Public Property Get Files_List()
    Set Files_List = mFiles_List
  End Property
  
End Class
 
.Files_List()(1)

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
The proper indexing and initializing should better be appearing like this.
[tt]
Private Sub Class_Initialize()
ReDim mFiles_List([red]-1[/red])
[blue]mFiles_List_Counter=0[/blue]
End Sub

Public Property Let Files_List(ByVal vData)
mFiles_List_Counter = mFiles_List_Counter + 1
ReDim Preserve mFiles_List(mFiles_List_Counter[red]-1[/red])
mFiles_List(mFiles_List_Counter[red]-1[/red]) = vData
End Property
[/tt]
 
dm4ever: THANK YOU! That is exactly correct, and I never would've thought to try it that way.

tsuji: thanks, I knew my implementation was a bit sloppy (and the utilization is even uglier); that will help me clean it up a bit.

skie: the dictionary object looks quite useful and a bit clearer than arrays - thanks for the tip. I may begin using that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top