VB6 - Object Variable or With Block Variable not Set

Started by
1 comment, last by orcfan32 18 years, 6 months ago
My code:
    Dim ItemList(5000) As Items
    CIndex& = 0

    While CIndex& <= MaxItems
        ItemList(CIndex&).ItemName = GetINISetting("ITEM" & CStr(CIndex&), _ ' ERROR IN THIS LINE
                                                   "Name", App.Path & "\Settings\GameData\Items.ini")
        lblInfo.Caption = "Loaded Item " & CStr(CIndex&) & ", Name"
        ItemList(CIndex&).ItemType = GetINISetting("ITEM" & CStr(CIndex&), _
                                                   "Type", App.Path & "\Settings\GameData\Items.ini")
        lblInfo.Caption = "Loaded Item " & CStr(CIndex&) & ", Type"
        ItemList(CIndex&).ItemTypeVal = GetINISetting("ITEM" & CStr(CIndex&), _
                                                      "TypeVal", App.Path & "\Settings\GameData\Items.ini")
        lblInfo.Caption = "Loaded Item " & CStr(CIndex&) & ", TypeVal"
        ItemList(CIndex&).ItemType = GetINISetting("ITEM" & CStr(CIndex&), _
                                                   "Class", App.Path & "\Settings\GameData\Items.ini")
        lblInfo.Caption = "Loaded Item " & CStr(CIndex&) & ", Class"
        ItemList(CIndex&).ItemType = GetINISetting("ITEM" & CStr(CIndex&), _
                                                   "Cost", App.Path & "\Settings\GameData\Items.ini")
        lblInfo.Caption = "Loaded Item " & CStr(CIndex&) & ", Cost"
        ItemList(CIndex&).ItemType = GetINISetting("ITEM" & CStr(CIndex&), _
                                                   "GFX", App.Path & "\Settings\GameData\Items.ini")
        lblInfo.Caption = "Loaded Item " & CStr(CIndex&) & ", GFX"
        
        lblInfo.Caption = "Loaded Item " & CStr(CIndex&)
        
        barProgress.Value = barProgress.Value + 1
        CIndex& = CIndex& + 1
    Wend



Items Class:
[source lange="vb"]'local variable(s) to hold property value(s)
Private mvarItemName As String 'local copy
Private mvarItemType As Integer 'local copy
Private mvarItemTypeVal As Integer 'local copy
Private mvarItemClass As Integer 'local copy
Private mvarItemCost As Long 'local copy
Private mvarItemGFX As Long 'local copy
Public Property Let ItemGFX(ByVal vData As Long)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.ItemGFX = 5
    mvarItemGFX = vData
End Property


Public Property Get ItemGFX() As Long
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.ItemGFX
    ItemGFX = mvarItemGFX
End Property



Public Property Let ItemCost(ByVal vData As Long)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.ItemCost = 5
    mvarItemCost = vData
End Property


Public Property Get ItemCost() As Long
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.ItemCost
    ItemCost = mvarItemCost
End Property



Public Property Let ItemClass(ByVal vData As Integer)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.ItemClass = 5
    mvarItemClass = vData
End Property


Public Property Get ItemClass() As Integer
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.ItemClass
    ItemClass = mvarItemClass
End Property



Public Property Let ItemTypeVal(ByVal vData As Integer)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.ItemTypeVal = 5
    mvarItemTypeVal = vData
End Property


Public Property Get ItemTypeVal() As Integer
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.ItemTypeVal
    ItemTypeVal = mvarItemTypeVal
End Property



Public Property Let ItemType(ByVal vData As Integer)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.ItemType = 5
    mvarItemType = vData
End Property


Public Property Get ItemType() As Integer
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.ItemType
    ItemType = mvarItemType
End Property



Public Property Let ItemName(ByVal vData As String)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.ItemName = 5
    mvarItemName = vData
End Property


Public Property Get ItemName() As String
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.ItemName
    ItemName = mvarItemName
End Property



Function for reading the INI:
Private Function GetINISetting(Section As String, KeyName As String, _
                               Path As String) As String
    RetVal$ = Space$(128)
    Size& = Len(RetVal$)
    
    GetPrivateProfileString Section, KeyName, "0", RetVal$, Size&, Path
    
    GetINISetting = RetVal$
End Function

Also, the kernel32, GetPrivateProfileString is declared as public in a module. I don't know why it is doing this. The class was built with the Class Wizard, so I don't know if its an issue with the class or the code. The parameters are all set and are correct and valid. Any suggestions? [Edited by - orcfan32 on October 20, 2005 9:02:12 PM]
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
Advertisement
In Visual Basic 6, variables of type Object are basically garbage-collected pointers. By default, they do not point to an instance of an object, you have to create an instance of that class and assign it using the Set statement.

In short, you will have to initialize the array:
    Dim ItemList(5000) As Items    'This is the important bit    Dim i As Long    For i = LBound(ItemList) To UBound(ItemList)        Set ItemList(i) = New Items    Next i    CIndex& = 0    While CIndex& <= MaxItems        ItemList(CIndex&).ItemName = GetINISetting("ITEM" & CStr(CIndex&), _ ' ERROR IN THIS LINE                                                   "Name", App.Path & "\Settings\GameData\Items.ini")        lblInfo.Caption = "Loaded Item " & CStr(CIndex&) & ", Name"        ItemList(CIndex&).ItemType = GetINISetting("ITEM" & CStr(CIndex&), _
I tried "Set Items(5000) = New Items", but that didn't work. But if I try that, it works. Thanks, MrEvil!
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit

This topic is closed to new replies.

Advertisement