• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

Help with Split...

Working on an RPG.
For the items, I have a text file with the format
ID# <> NameofItem <> TypeofItem <> ValueofItem
To test the call, I made a sample form with a text box and a command button.
Code:

Private Sub cmdNewItem_Click()
Item = txtItem.Text
If Item = "" Then Item = 0
Call Module1.CreateItem(Item)

End Sub

Module1.CreateItem:
Sub CreateItem(ByVal ItemNo As Integer)
fileItems = App.Path + "\Data\Items.dcl"
If ItemNo = 0 Then ItemNo = 100
If ItemNo < 100 Or ItemNo > 300 Then
MsgBox "Invalid Item ID Detected(" & ItemNo & ")"
End
End If
'Note About File Set Up
'fileItems format for items is as follows:
'ID% <> Name <> Type <> Value
'Icon = ID%
Open fileItems For Input As #1
Do Until EOF(1)
    Line Input #1, Temp$
    ID% = Split(Temp$, "<>")(0)
    If ItemNo = ID% Then
        Items(ItemNo).ID = Split(Temp$, "<>")(0)
        Items(ItemNo).Name = Split(Temp$, "<>")(1)
        Items(ItemNo).Type = Split(Temp$, "<>")(2)
        Items(ItemNo).Value = Split(Temp$, "<>")(3)
        Items(ItemNo).Icon = ID%
        If Items(ItemNo).Type = "Armor" Then
            MsgBox "You found a suit of " & Items(ItemNo).Name & "!"
        Else
            MsgBox "You found a " & Items(ItemNo).Name & "!"
        End If
        Close #1
        Universe.NoOfItems = Universe.NoOfItems + 1
        Exit Sub
    End If
Loop
Close #1
End Sub

Every time I try to run this little tidbit...if I set the ID equal to 100 by any means, it's give me the appropriate "You found a Dagger!" MsgBox. It also recognizes the invalid IDs (below 100, above 300). Any number above one hundred and below three hundred, the accepted range, however, gives me a "Subscript out of range" error. Debug points at the line "Items(ItemNo).ID = Split(Temp$, "<>")(0)" in CreateItem. This is the second time I've had split errors in a program; the last tiem it happened I just had to sit and toy with it until i foudn some thick, bulky code to cover it up. Is there anyone who knwos how to fix this, or who can at least pass me a fair reason why the subcript is out of range? I'd be greatly appreciative for any help.
0
Aioreyn
Asked:
Aioreyn
  • 2
1 Solution
 
stefriCommented:
Where do you Dimension Items? WHat is its size?

If I understand what yo are doing, Items should be an array which is at least 300 long as:
if ItemNo = 0 you force it to 100 and write in items(100).ID the found value from your file and itemNo can at most be equal to 300 and you will write in items(300)

Apparently, if Dagger is 100, Items must have been declared as dim items(100) (101 slots)

Am I right?

Stefri

0
 
AioreynAuthor Commented:
The Dims arei n the same module, and are as follows:

Public Type ItemArray
    ID As Integer
    Name As String
    Type As String
    Icon As Integer
    Value As Integer
    Special As Integer
    Magic As Integer
    Equipped As Boolean
End Type
Global Items(100) As ItemArray
0
 
stefriCommented:
If the read value is 120, you will write it in slot 120 of Items which is limited to slot 100, so it will bomb with Subscript out of range" error.

Increase the size of Items or write at slot ItemID - ubound(Items)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now