Index was out of range
Index was out of range
Index was out of range
Index was out of range
Index was out of range
Index was out of range Index was out of range Index was out of range Index was out of range Index was out of range Index was out of range Index was out of range Index was out of range
Index was out of range Index was out of range
Index was out of range
Go Back  Xtreme Visual Basic Talk > > > Index was out of range


Reply
 
Thread Tools Display Modes
  #1  
Old 01-11-2014, 09:06 AM
JeremyBenson11 JeremyBenson11 is offline
Regular
 
Join Date: Dec 2013
Posts: 50
Default Index was out of range


I got working on a save file in xml but but I'm getting an index out of bounds. It happens when it gets to the traits. I'm assuming since the habits are set up the same way it'll give the same exception when it gets to it, but I'm not sure.

I know I'm using some horrible methods here, but what ever floats the boat eh? lol

I'm not sure why I'm getting the exception from though because I'm getting the amount of loop iterations from a count of items in the list of traits so I must just be making some simple mistake here.

Code:
  Dim amountOfCharacters As SByte = characterHolder.characterName.Count
            Dim traitCount As SByte = characterHolder.characterTraits.Count
            Dim habitCount As SByte = characterHolder.characterHabits.Count
            Dim traitCounter As SByte = 0
            Dim habitCounter As SByte = 0
            Dim counter As SByte = 0

            writer.WriteStartDocument()
            writer.WriteStartElement("project") ' Root.

            writer.WriteStartElement("book")
            writer.WriteElementString("title", ProjectDataMod.projectNameMod)
            writer.WriteElementString("chapters", ProjectDataMod.projectChaptersMod)
            writer.WriteElementString("path", ProjectDataMod.projectPathMod)
            writer.WriteEndElement()
            writer.WriteStartElement("characters")
            Do While counter < amountOfCharacters
                writer.WriteElementString("name", characterHolder.characterName.AsReadOnly(counter))
                writer.WriteElementString("race", characterHolder.characterRace.AsReadOnly(counter))
                writer.WriteElementString("age", characterHolder.characterAge.AsReadOnly(counter))
                writer.WriteElementString("gender", characterHolder.characterGender.AsReadOnly(counter))
                writer.WriteElementString("origin", characterHolder.characterOrigin.AsReadOnly(counter))
                writer.WriteElementString("eyecolor", characterHolder.characterEyeColor.AsReadOnly(counter))
                writer.WriteElementString("haircolor", characterHolder.characterHairColor.AsReadOnly(counter))
                writer.WriteElementString("height", characterHolder.characterHeight.AsReadOnly(counter))
                writer.WriteElementString("weight", characterHolder.characterWeight.AsReadOnly(counter))
                writer.WriteElementString("occupation", characterHolder.characterOccupation.AsReadOnly(counter))
                writer.WriteElementString("birthmarks", characterHolder.characterBirthmarks.AsReadOnly(counter))
                writer.WriteElementString("piercing", characterHolder.characterPiercings.AsReadOnly(counter))
                writer.WriteElementString("tattoos", characterHolder.characterTattoos.AsReadOnly(counter))
                writer.WriteElementString("scars", characterHolder.characterScars.AsReadOnly(counter))

                Do While traitCounter < traitCount
                    writer.WriteElementString("trait" + CStr(traitCounter), characterHolder.characterScars.AsReadOnly(traitCounter))

                    traitCounter = traitCounter + 1

                Loop

                Do While habitCounter < habitCount

                    writer.WriteElementString("habit" + CStr(habitCounter), characterHolder.characterScars.AsReadOnly(habitCounter))

                    habitCounter = habitCounter + 1

                Loop

                counter = counter + 1
            Loop
            writer.WriteEndElement()
            writer.WriteEndDocument()
Reply With Quote
  #2  
Old 01-11-2014, 04:21 PM
PlausiblyDamp's Avatar
PlausiblyDampIndex was out of range PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Have you tried setting a breakpoint and stepping through your code, just to see what values you are getting and if they match up with reality.

Also if you are writing XML you really don't need to create elements with unique names by adding an increment to each one, it might be easier to just simply have multiple <trait>...</trait> elements rather than numbering them.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 01-13-2014, 10:59 AM
JeremyBenson11 JeremyBenson11 is offline
Regular
 
Join Date: Dec 2013
Posts: 50
Default

This is true. I can't really figure out how to use this method properly... Not supposed to be doing it this way I don't think, lol.... I have to delete this class... I feel like I need to learn how to write out the character's data out to an XML file... and then learn how to insert a new character into the XML file without using this confusing method...

If I use the way I'm using now I have to find some way of remembering indexes and where each character's data is placed in the holder class.. Seems like an improper mess..

What I'm doing now is feeding a classes data to another class of lists and then writing the list data out to xml. I thought it would be and easy way of doing it but after treading there a bit I discovered it to be a muddled mess, lo.

Just having a hard time finding valuable learning resources.
Reply With Quote
  #4  
Old 01-13-2014, 02:23 PM
PlausiblyDamp's Avatar
PlausiblyDampIndex was out of range PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

If you are just trying to turn an instance of a .Net class into XML and reverse the process later you might want to look at XMLSerialization (http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx) as that can take a lot of the effort out of generating and reading XML.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #5  
Old 01-15-2014, 09:16 AM
JeremyBenson11 JeremyBenson11 is offline
Regular
 
Join Date: Dec 2013
Posts: 50
Default

lol, I just learned how to do what I wanted with Xmldocument. It's a bit tricky learning, but I got the first part down. I kinda need to stick to one xml file because I'm gonna use that as a means to draw save-file type data from.

the method below does what I was trying to do. I just have to add some logic so that instead of creating a character element every time it appends a characters element into the character element when the element characters exists.

Code:
 Dim traitCount As SByte = characterTraits.Count
        Dim habitCount As SByte = characterHabits.Count
        Dim counter As SByte = 0

        Dim document As New XmlDocument
        document.Load(ProjectDataMod.projectPathMod + ProjectDataMod.projectNameMod + ".xml")
        Dim nav As Xml.XPath.XPathNavigator = document.CreateNavigator

        nav = nav.SelectSingleNode("/project/book")
        nav.InsertElementAfter("", "characters", "", "")
        nav = nav.SelectSingleNode("/project/characters")
        nav.AppendChildElement("", "character", "", "")
        nav.MoveToChild(XPath.XPathNodeType.Element)
        nav.AppendChildElement("", "name", "", characterName)
        nav.AppendChildElement("", "race", "", characterRace)
        nav.AppendChildElement("", "age", "", characterAge)
        nav.AppendChildElement("", "gender", "", characterGender)
        nav.AppendChildElement("", "origin", "", characterOrigin)
        nav.AppendChildElement("", "eye", "", characterEyeColor)
        nav.AppendChildElement("", "hair", "", characterHairColor)
        nav.AppendChildElement("", "weight", "", characterWeight)
        nav.AppendChildElement("", "height", "", characterHeight)
        nav.AppendChildElement("", "occupation", "", characterOccupation)
        nav.AppendChildElement("", "birthmarks", "", characterBirthmarks)
        nav.AppendChildElement("", "piercings", "", characterPiercings)
        nav.AppendChildElement("", "tattoos", "", characterTattoos)
        nav.AppendChildElement("", "scars", "", characterScars)

        Do While counter < traitCount

            nav.AppendChildElement("", "trait", "", characterTraits.AsReadOnly(counter))

            counter = counter + 1
        Loop

        counter = 0

        Do While counter < habitCount

            nav.AppendChildElement("", "habit", "", characterHabits.AsReadOnly(counter))

            counter = counter + 1
        Loop

        nav.AppendChildElement("", "bio", "", characterBio)


        document.Save(ProjectDataMod.projectPathMod + ProjectDataMod.projectNameMod + ".xml")
Reply With Quote
  #6  
Old 01-16-2014, 11:41 AM
JeremyBenson11 JeremyBenson11 is offline
Regular
 
Join Date: Dec 2013
Posts: 50
Default

I can't seem to figure how how to append a child element to the end of an element and append grandchildren inside that.

The document is like this

<project>
<book>
</book>
<characters>
<character>
<name></name>
<race></race>
</character>
--Need to append character here
</characters>
</project>

What I have so far is..

Code:
nav = nav.SelectSingleNode("/project/characters")
            nav.AppendChildElement("", "character", "", "")
            nav.SelectSingleNode("//project/characters/*[last()]")
            nav.AppendChildElement("", "name", "", characterName)
            nav.AppendChildElement("", "race", "", characterRace)
            nav.AppendChildElement("", "age", "", characterAge)
it appends the character but it doesn't write the data inside the new element. No matter what I do I can seem to get inside the newly created element, lol.
Reply With Quote
  #7  
Old 01-17-2014, 08:58 AM
PlausiblyDamp's Avatar
PlausiblyDampIndex was out of range PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

When you do
Code:
nav = nav.SelectSingleNode("/project/characters")
nav.AppendChildElement("", "character", "", "")
nav.SelectSingleNode("//project/characters/*[last()]")
I think it means nav still points to the /project/characters element and not the newly added character.

Shouldn't it be
Code:
nav = nav.SelectSingleNode("/project/characters")
nav.AppendChildElement("", "character", "", "")
nav = nav.SelectSingleNode("//project/characters/*[last()]")
instead? I could very well be wrong however as it has been a while since I used XmlDocuments.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #8  
Old 01-17-2014, 10:24 AM
JeremyBenson11 JeremyBenson11 is offline
Regular
 
Join Date: Dec 2013
Posts: 50
Default

Amazing. Xml is being created exactly as I was hoping it would be.

Thanks for all the help here.

Just out of curiosity where can I learn more about the expressions this function can take?

nav.SelectSingleNode("//project/characters/*[last()]")

looks a bit like regex?
Reply With Quote
  #9  
Old 01-17-2014, 01:55 PM
PlausiblyDamp's Avatar
PlausiblyDampIndex was out of range PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

http://www.w3schools.com/xpath/ is a pretty decent introduction.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #10  
Old 01-22-2014, 10:51 AM
JeremyBenson11 JeremyBenson11 is offline
Regular
 
Join Date: Dec 2013
Posts: 50
Default

Awesome thanks. Just skimming the syntax and learned a bit
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Index was out of range
Index was out of range
Index was out of range Index was out of range
Index was out of range
Index was out of range
Index was out of range Index was out of range Index was out of range Index was out of range Index was out of range Index was out of range Index was out of range
Index was out of range
Index was out of range
 
Index was out of range
Index was out of range
 
-->