vbscript array of object has no values

SamTrexler
SamTrexler used Ask the Experts™
on
I am following the code in the "object array in vbscript" solution.  My code runs and produces output, but the individual object properties in each element of the array are not receiving values.  Can someone point out my error?  Code file attached.  Thanks.
TestArraysContainingClasses.vbs
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014
Commented:
Hi, I have fixed up your code.  I don't think you're GetServerName function is required, seeing as you can use
ServerName = ServerArray(ServerIndex).ServerName
anywhere in your code anyway.  You could just put the validation inside the
  Public Property Get ServerName

if you need to.

I think the main problem you were having was due to your Let blocks

  Public Property Let ServerName (i_ServerName)
    m_ServerName = i_ServerName
  End Property

where you had     m_ServerName = ServerName    and NOT     m_ServerName = i_ServerName

Regards,

Rob.
Option Explicit

Class ServerClass

  Private m_ServerName
  Private m_ServerStatus
  Private m_ServerStatusTime

  Public Property Get ServerName
    ServerName = m_ServerName
  End Property

  Public Property Let ServerName (i_ServerName)
    m_ServerName = i_ServerName
  End Property

  Public Property Get ServerStatus
    ServerStatus = m_ServerStatus
  End Property

  Public Property Let ServerStatus (i_ServerStatus)
    m_ServerStatus = i_ServerStatus
  End Property

  Public Property Get ServerStatusTime
    ServerStatusTime = m_ServerStatusTime
  End Property

  Public Property Let ServerStatusTime (i_ServerStatusTime)
    m_ServerStatusTime = i_ServerStatusTime
  End Property

End Class

'Function GetServerName(ByVal i_Index)

'    If i_Index < LBound(ServerArray) Or i_Index > UBound(ServerArray) Then 
'      Exit Function
'    End If

'    ServerName= ServerArray(i_Index).ServerName

'End Function ' GetServerName(ByVal i_Index)

Sub AddServerName (ByVal i_Index, ByVal i_ServerName)

  WScript.Echo "Setting ServerName at Index " & i_Index & " to " & i_ServerName

  If i_Index < LBound(ServerArray) Then
    Exit Sub
  End If

  If i_Index > UBound(ServerArray) Then

    ReDim Preserve ServerArray(i_Index)
    WScript.Echo "ServerArray Redim to index " & i_Index & " complete"

    Set ServerArray(i_Index) = New ServerClass
    WScript.Echo "New instance of ServerClass created for ServerArray(" & i_Index & ")"

  End If

  WScript.Echo "Before Setting ServerName at Index " & i_Index & " to " & i_ServerName

  ServerArray(i_Index).ServerName = i_ServerName
  ServerArray(i_Index).ServerStatus = "Unknown"
  ServerArray(i_Index).ServerStatusTime = Now

  WScript.Echo "After Setting ServerName at Index " & i_Index & " to " & i_ServerName
  WSCript.Echo "Value in array at index " & i_Index & " is " & ServerArray(i_Index).ServerName & _
               " with status " & ServerArray(i_Index).ServerStatus & " at " & ServerArray(i_Index).ServerStatusTime

End Sub ' AddServerName (ByVal i_Index, ByVal i_ServerName)

Function GetMaxIndex
  GetMaxIndex = UBound(ServerArray)
End Function ' MaxIndex()

Dim ServerArray()
Dim ServerName, ServerIndex, ServerMaxIndex

ReDim ServerArray(0)
WScript.Echo "ServerArray Redim to 0 complete"

Set ServerArray(0) = New ServerClass
WScript.Echo "New instance of ServerClass created for ServerArray(0)"

ServerName = "Server1"
ServerIndex = 0
WScript.Echo ""
WScript.Echo "Server Name " & ServerName & " has Index " & ServerIndex
AddServerName ServerIndex, ServerName

ServerName = "Server2"
ServerIndex = 1
WScript.Echo ""
WScript.Echo "Server Name " & ServerName & " has Index " & ServerIndex
AddServerName ServerIndex, ServerName

ServerName = "Server3"
ServerIndex = 2
WScript.Echo ""
WScript.Echo "Server Name " & ServerName & " has Index " & ServerIndex
AddServerName ServerIndex, ServerName

WScript.Echo ""
WScript.Echo "Get Max Index"
ServerMaxIndex = GetMaxIndex

WScript.Echo "Loop through array"
For ServerIndex = 0 to ServerMaxIndex

  WScript.Echo "ServerArrary at Index " & ServerIndex & _
               " has value " & ServerArray(ServerIndex).ServerName

  ServerName = ServerArray(ServerIndex).ServerName
  WScript.Echo "GetServerName for Index " & ServerIndex & _
               " returns value " & ServerName

Next
WScript.Echo "End of loop"

Open in new window

Author

Commented:
Thanks, Rob, that did it.

I was using the Let statements to try to encapsulate code based on an article at http://www.4guysfromrolla.com, but your probably right, it doesn't look like that will buy me anything even when I get all the application logic in here.

Regards,
Sam
Most Valuable Expert 2012
Top Expert 2014

Commented:
The Let statements will work, and make for good Class coding practice, you only had the parameter name incorrect when setting the value.

Thanks for the grade.

Regarsd,

Rob.

Author

Commented:
Awesome.  That works great, and thanks for the guidance.

Regards,

Sam

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial