Page 1 of 2 12 LastLast
Results 1 to 40 of 45

Thread: Saving and loading

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Question Saving and loading

    What I want to do is have my form save and load the 27 fields I have, but I want to close the program and it save and load to a file but I dont know how to have it write or read from a file. Please help me.
    If there is somebody here more of a noob you can have my spot as noob.

  2. #2
    Addicted Member
    Join Date
    May 2005
    Posts
    162

    Re: Saving and loading

    do you know anything about streamreaders and writers?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Re: Saving and loading

    No I don't
    If there is somebody here more of a noob you can have my spot as noob.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Re: Saving and loading

    this is what my form looks like
    Attached Images Attached Images  
    If there is somebody here more of a noob you can have my spot as noob.

  5. #5
    Addicted Member techwizz's Avatar
    Join Date
    Apr 2005
    Location
    U.S.A.
    Posts
    246

    Re: Saving and loading

    wow thats a cool form

  6. #6
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Saving and loading

    Quote Originally Posted by Kesk
    What I want to do is have my form save and load the 27 fields I have, but I want to close the program and it save and load to a file but I dont know how to have it write or read from a file. Please help me.
    here's a sample on how to read and write a txt file
    VB Code:
    1. Imports System.IO
    2. Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    3.         Dim s As New StreamWriter("f:/test1.txt")
    4.         Dim o As String
    5.         For Each o In ListBox1.Items
    6.             s.WriteLine(o)
    7.         Next
    8.         MessageBox.Show("file has been successfully saved")
    9.         s.Close()
    10.     End Sub
    11.  
    12.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    13.         Dim s As New StreamReader("f:/test1.txt")
    14.         s.BaseStream.Seek(0, SeekOrigin.Begin)
    15.         While s.Peek > -1
    16.             ListBox2.Items.Add(s.ReadLine)
    17.         End While
    18.         s.Close()
    19.     End Sub

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Re: Saving and loading

    So does that save all the fields information then?? I have a few more I added fields to save, so would I have to add more code??
    If there is somebody here more of a noob you can have my spot as noob.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Re: Saving and loading

    thanks techwizz
    If there is somebody here more of a noob you can have my spot as noob.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving and loading

    There is nothing wrong with mar_zim's suggestion, but I think it would be easier overall to create a class to hold all your values in properties and then serialize an instance of your class to and from an XML file.

  10. #10
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Saving and loading

    Quote Originally Posted by jmcilhinney
    There is nothing wrong with mar_zim's suggestion, but I think it would be easier overall to create a class to hold all your values in properties and then serialize an instance of your class to and from an XML file.
    I agree. Classes all the way. A class would be a perfect way to do this, because it centralizes all the data in one object that you can pass back and forth in one line of code
    I don't live here any more.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Re: Saving and loading

    so how would i do that, I have only been programmin for a few months
    If there is somebody here more of a noob you can have my spot as noob.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving and loading

    See this thread for an example of how I accomplish this task. Some others may have slightly different ideas but this method always works for me.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Re: Saving and loading

    but i want to save to one file and load from it
    If there is somebody here more of a noob you can have my spot as noob.

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving and loading

    The method I suggested does save to one file. You create a class that stores all the values you want to save. I always use the preferred practice of storing each value in a private variable and exposing it through a public property. You then serialize the entires object to an XML file, which takes four lines of code. To load your data, you deserialize your XML file to an object, which also takes four lines of code. There are various advantages to this method. Because the serialization/deserialization mechanism is built into the .NET Framework, you only need those four lines of code regardless of how many values you want to save and what type they are. They can even be complex objects themselves, not just integers and strings. Also, because you have all your values contained in a single object, you can easily pass them around your app as a single entity.

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Re: Saving and loading

    how would i change that to take advantage of mine??
    If there is somebody here more of a noob you can have my spot as noob.

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving and loading

    I will create an abbreviated example. First you add a new class to your project and you give it properties that correspond to all the values displayed in your dialogue box. Next you write methods to load and save a character. Where you put them depends on you and your project, but it would make sense to make them members of your class. If you do that, the Load method should be Shared so that you don't need a character instance already to load one.
    VB Code:
    1. Public Class Character
    2.  
    3.     'Store actual values in private variables.
    4.     Private m_Name As String
    5.     Private m_Age As UInt32
    6.     Private m_Class As String
    7.     'etc.
    8.  
    9.     'Expose values through public properties to allow for read-only and future change of implementation.
    10.  
    11.     Public Property Name() As String
    12.         Get
    13.             Return Me.m_Name
    14.         End Get
    15.         Set(ByVal Value As String)
    16.             Me.m_Name = Value
    17.         End Set
    18.     End Property
    19.  
    20.     'Note that it may be better to store the date of birth and then calculate the age dynamically if real time is desired.
    21.     Public Property Age() As UInt32
    22.         Get
    23.             Return Me.m_Age
    24.         End Get
    25.         Set(ByVal Value As UInt32)
    26.             Me.m_Age = Value
    27.         End Set
    28.     End Property
    29.  
    30.     'Note that "Class" is a reserved word so to use it as an identifier it must be enclosed in square brackets.
    31.     'If specific character classes are already defined you may want to use an enumeration instead of a string.
    32.     Public Property [Class]() As String
    33.         Get
    34.             Return Me.m_Class
    35.         End Get
    36.         Set(ByVal Value As String)
    37.             Me.m_Class = Value
    38.         End Set
    39.     End Property
    40.  
    41.     'etc.
    42.  
    43.     'The fileName parameter is the fully qualified path to where the file should be saved, e.g. "C:\Character.xml".
    44.     Public Sub Save(ByVal fileName As String)
    45.         Dim serialiser As New Xml.Serialization.XmlSerializer(Me.GetType())
    46.         Dim stream As New IO.FileStream(fileName, IO.FileMode.Create) 'An exception will be thrown if the file cannot be created.
    47.  
    48.         serialiser.Serialize(stream, Me)
    49.         stream.Close()
    50.     End Sub
    51.  
    52.     'The fileName parameter is the fully qualified path to the file that should be loaded, e.g. "C:\Character.xml".
    53.     Public Shared Function Load(ByVal fileName As String) As Character
    54.         If IO.File.Exists(fileName) Then
    55.             'Load the character from the specified file.
    56.             Dim serialiser As New Xml.Serialization.XmlSerializer(GetType(Character)) '"Me" cannot be used in a shared method.
    57.             Dim stream As New IO.FileStream(fileName, IO.FileMode.Open) 'An exception will be thrown if the file does not exist.
    58.  
    59.             'A function contains an implicit local variable of the same name and type that can be assigned to in place of using a return statement.
    60.             Load = serialiser.Deserialize(stream)
    61.             stream.Close()
    62.         Else
    63.             'Create a new, empty character.
    64.             Return New Character
    65.         End If
    66.     End Function
    67.  
    68. End Class

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Re: Saving and loading

    under the save and load buttons i would have it so it is linked to the class and finds the stuff it needs but i dont know how to do that
    If there is somebody here more of a noob you can have my spot as noob.

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving and loading

    The Click event handler for your Load button will call Character.Load(<path>), which returns a Character object. You then use the properties of the Character object to populate the controls of your dialogue. Note that if there is no file at the specified location, which is likely the first time you run this, an empty Character, or at least one with default values, will be returned. Also note that Load is a Shared method, so it is (normally) called using the class name.

    The Click event handler for your Save button will get a Character object (it's up to you whether you use the same one returned by Load or a new, blank object) and set its properties from the controls of your dialogue. You would then call Save(<path>) on that instance to Save the character. Note that Save is not a Shared method so it must be called on an actual Character object, e.g. myCharacter.Save(<path>).

    Note that XML files from serialised objects have a specific structure. If you don't know exactly what that structure is then you should let the first run of your program create a new character and then saving it will create the file for the first time. You may also want to get the file path by some other method than passing it as an argument. You might want to set it as a constant somewhere and have the Save and Load methods retrieve it themselves. That's your choice and depends what works best in your case.

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Re: Saving and loading

    well this is a final project in my programming class practically due tomorrow for me and im not sure which case or how to implement it would work i've been programming for like 3 months
    If there is somebody here more of a noob you can have my spot as noob.

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving and loading

    I see you are using menu items rather than buttons. I will create a demo that will update the Name, Class and Strength values and post it here very soon. It's up to you to extend the principle to include the other character properties. Check back in about 30-60 minutes.

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Re: Saving and loading

    ok
    thank you very much
    If there is somebody here more of a noob you can have my spot as noob.

  22. #22
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving and loading

    Here is the demo project as promised. I have used VS.NET 2003. I didn't check what version you were using.

    A few points to note:
    • This project saves and loads the same file from the same location each time.
    • There is no checking to make sure that required fields have been populated.
    • There is no confirmation if the user tries to create a new character or load a character while they are editing one.
    This project is purely to demonstrate the serialisation techniques I suggested. Any issues, let me know.

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Re: Saving and loading

    is there a way to have them input file name to load and save
    If there is somebody here more of a noob you can have my spot as noob.

  24. #24
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving and loading

    You should use the OpenFileDialog control when loading and the SaveFileDialog control when saving. You should set the Filter property to limit the types of files they can see. Note that you don't have to use the .xml extension. You can use your own extension if you want without affecting the contents of the file. Once the user has made their choice you pass the FileName property of the dialogue box as the path to the Save or Load method.

  25. #25
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving and loading

    I've update the project to use the FileDialogs. Note that they both refer to CXF files (character XML file). This is an extension that probably isn't in use by any other common application and it prevents the user trying to load some other XML file that isn't a valid character.

  26. #26
    Lively Member DanielS1324's Avatar
    Join Date
    Apr 2005
    Posts
    79

    Re: Saving and loading

    jmcilhinney... Been following along here.

    Great info... Now I have a question.

    What if I need to store more than 1 class in the file. Or even an array of classes. Saving them is easy, but how do I load certian ones? In other words... If I have classes a, b, c, d, e, and f, They're all saved to the xml file. Now if I want to load class e, how do I do that?

    Also, how can I tell how many classes are stored in the file?

    Even another one... how would I delete class e?

  27. #27
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving and loading

    Quote Originally Posted by DanielS1324
    jmcilhinney... Been following along here.

    Great info... Now I have a question.

    What if I need to store more than 1 class in the file. Or even an array of classes. Saving them is easy, but how do I load certian ones? In other words... If I have classes a, b, c, d, e, and f, They're all saved to the xml file. Now if I want to load class e, how do I do that?

    Also, how can I tell how many classes are stored in the file?

    Even another one... how would I delete class e?
    I'm sure I don't know all there is to serialisation. What I've demonstrated here is really the basics. There is definitely plenty I don't know about the use of XML in general. That said, as far as I'm aware it is not possible to deserialise part of a file. If you want one object amongst many that have been serialised to a single file, I believe you would have to deserialise the whole file and extract the single object you need. The same goes for deleting, in that you would have to deserialise, delete and then reserialise. Although I've never tried it, I don't see why you couldn't use the XmlDocument class to manipulate the XML directly. I think that this method would be more fiddly and more error-prone, though, and I don't think that it would be considerably less expensive with respect to resources and time.

  28. #28

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Re: Saving and loading

    i'm getting an error at the underlined part

    VB Code:
    1. Public Property BACost() As String
    2.         Get
    3.             Return Me.BACost
    4.         End Get
    5.         Set(ByVal Value As String)
    6.             [U]Me.BACost = Value[/U]
    7.         End Set
    8.     End Property

    "An unhandled exception of type 'System.StackOverflowException' occurred in Character Creator.exe" is the error and it's highlighted in yellow if that helps
    If there is somebody here more of a noob you can have my spot as noob.

  29. #29
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: Saving and loading

    @Kesk

    By setting Me.BACost you are creating a infinite loop.

    VB Code:
    1. Private m_BACoast As String = ""
    2.      Public Property BACost() As String
    3.         Get
    4.             Return m_BACoast 'returning the privately held m_BACost
    5.         End Get
    6.         Set(ByVal Value As String)
    7.             m_BACoast  = Value 'Setting m_BACost rather than Me.BACost
    8.         End Set
    9.     End Property
    Last edited by grilkip; Jun 9th, 2005 at 07:41 PM.
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

  30. #30
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving and loading

    Exactly as grilkip says. I usually name my variables the same as their corresponding property with a "m_" prefix because it indicates the relationship between the two. You are the second person to make this mistake when adapting my code. Perhaps I will make the distinction clearer in future. If the similarity between the names confuses you at all you can certainly use a different naming convention to mine.

    Edit:
    The "m_" prefix is to indicate a member variable as opposed to a local one. This type of naming convention is nto so necessary these days as IntelliSense gives you that type of information on mouse-over. I still use this particular throwback because it is a convenient way to show that a variable is related to a property. If you aren't too familiar with properties, you may be wondering why they should be used at all instead of just making the variables themselves public. Properties do several things for the developer that variables do not. They can be declared read-only, so they can return a value without allowing it to be updated. They allow calculated values when getting and complex operations when setting. They also allow you to change the implementation of a property without changing the interface. They basically combine the good bits of variables and methods into a single entity.
    Last edited by jmcilhinney; Jun 9th, 2005 at 08:00 PM.

  31. #31

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Re: Saving and loading

    When I put the "opendialogbox" code in it says that the opendialogbox isnt a member. how would i fix that??
    If there is somebody here more of a noob you can have my spot as noob.

  32. #32
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving and loading

    You have to have added the OpenFileDialog and the SaveFileDialog to your form in the designer. I kept the default names so they should match up, or you can change their names to something more descriptive in the designer and then do a Find and Replace in the code.
    Last edited by jmcilhinney; Jun 9th, 2005 at 10:25 PM. Reason: Spelling error

  33. #33

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Re: Saving and loading

    jmcilhinney would you like to see a final copy when i got it done??
    If there is somebody here more of a noob you can have my spot as noob.

  34. #34
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving and loading

    By all means, but make sure you post your project and not an executable. No one with sense (which I hope includes me) would download an executable from a developer forum thread. Also, if you could sign over all your royalties from distribution of the app

  35. #35

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Re: Saving and loading

    well i'll sign over half of them cause i won't be making a dime it's pretty much a final project that i didnt even know was a final project when i started 2 months ago.
    If there is somebody here more of a noob you can have my spot as noob.

  36. #36

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Re: Saving and loading

    And I can't figure out why its not showing the dialog boxes now
    If there is somebody here more of a noob you can have my spot as noob.

  37. #37
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving and loading

    Are you getting error messages? Is it refusing to compile? Try putting a break point somewhere before the dialogues are supposed to be shown and step through execution.

  38. #38

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Re: Saving and loading

    Now its just not saving to a file.
    Last edited by Kesk; Jun 9th, 2005 at 10:53 PM.
    If there is somebody here more of a noob you can have my spot as noob.

  39. #39
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Saving and loading

    Then debug it in the IDE and use a break point as I suggested. Put the cursor on a line before the dialogue gets shown and when the debugger stops at that point use F10 to step to the next line. Use F11 to step into a property or method and Shift+F11 to step out of the current property or method. If you're still not sure you can post some code.

  40. #40

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Location
    Hidden in a dark corner, of a dark room, in a dark house, on a dark island, on a dark planet.
    Posts
    92

    Re: Saving and loading

    its saving to the .xml file in the bin folder but wont create one
    If there is somebody here more of a noob you can have my spot as noob.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width