Object not getting set

  • Thread starter Thread starter Andrew Cooper
  • Start date Start date
A

Andrew Cooper

Greetings,

I'm running into a problem where an Object is evaluating to Nothing, even
though I'm setting it to a specific Object. Here's my code:

<code>
Private Sub CmdInventory(ByVal Sender As Client, ByVal Arg As String)
'Shows the inventory of the indicated player. If the player is anyone other
than
'the Sender then the Sender must be a Wizard or Royal to see it.
Dim oPlayer As Player
Dim oTarget As Player
Dim lDBRef As Long
Dim cInv As New Hashtable()
Dim oItem As Item
Dim sMsg As New CString()
'Must be logged on.
If Sender.Status < ClientStatusEnum.LoggedIn Then
Sender.Send("You must be logged in.")
Exit Sub
End If
'If the Arg is someone other than the Sender then the Sender must be
'a Wizard or Royal.
oPlayer = cPlayers.Item(Sender.Player)
If Arg.Length > 0 Then
lDBRef = cPlayers.PlayerExists(Arg)
If oPlayer.CheckName(Arg) = False And Arg.Compare(Arg.ToLower, "me") <>
0 Then
If Not oPlayer.CheckFlag(FlagEnum.Wizard + FlagEnum.Royal) Then
Sender.Send("You cannot view another player's inventory.")
Exit Sub
Else
'Check to see if the named player even exists.
If lDBRef = 0 Then
Sender.Send("That player does not exist; therefore, his
inventory does not exist.")
Exit Sub
End If
End If
End If
Else
lDBRef = Sender.Player
End If

'Get the Inventory and construct the Message.
oTarget = cPlayers.Item(lDBRef) <<<< This line seems to be setting oTarget
to Nothing, even though in the Debugger I can see that lDBRef
is the key
to an object in cPlayers, which is a Hashtable.
cInv = oTarget.GetInventory()
sMsg.Value = bRED & oTarget.Name & " Inventory:" & vbCrLf & bBLUE
If cInv.Count = 0 Then
sMsg.Append("Nothing" & vbCrLf)
Else
For Each oItem In cInv.Values
sMsg.Append(oItem.Name & " <" & oItem.DBRef & ">" & vbCrLf)
Next
End If
sMsg.Append(WHITE)
Sender.Send(sMsg.Value)
End Sub

</code>

Any help would be appreciated.

Andrew
 
I just reread my post and realized that the News Reader reformatted my code
badly and that I put too much code in the post. My apologies. I've cut
down the code a bit and am going to try to format it nicely... if the News
Reader will let me do it.

I'm running into a problem where an Object is evaluating to Nothing, even
though I'm setting it to a specific Object. Here's my code:

oPlayer = cPlayers.Item(Sender.Player)
If Arg.Length > 0 Then
lDBRef = cPlayers.PlayerExists(Arg)
If oPlayer.CheckName(Arg) = False And _
Arg.Compare(Arg.ToLower, "me") <> 0 Then

If Not oPlayer.CheckFlag(FlagEnum.Wizard + FlagEnum.Royal) Then
Sender.Send("You cannot view another player's inventory.")
Exit Sub
Else

'Check to see if the named player even exists.
If lDBRef = 0 Then
Sender.Send("That player does not exist.")
Exit Sub
End If
End If
End If
Else
lDBRef = Sender.Player
End If

'Get the Inventory and construct the Message.
'This line seems to be setting oTarget to Nothing,
'even though in the Debugger I can see that lDBRef
'is the key to an object in cPlayers, which is a Hashtable.
oTarget = cPlayers.Item(lDBRef)
cInv = oTarget.GetInventory()


There we go. Much better, I hope. Any help would be greatly appreciated.

Andrew
 
Is Sender.Player an integer? When the items are loaded are they added to the
hashtable using an integer key? I ask because lDBRef is a Long and one of
the dangers, one that most people overlook, of using a numeric key is that:

Dim Key1 as Byte = 25
Dim Key2 as Short = 25
Dim Key3 as Integer = 25
Dim Key4 as Long = 25

are all different keys since the object that is placed in the key collection
is of a different type.

So, if you are adding an item to cPlayer with an integer key of 12345 and
later you try to retrieve that item using a long key of 12345 the Hashtable
will, quite correctly, return Nothing.
 
Back
Top