Results 1 to 14 of 14

Thread: [RESOLVED] Storing very large strings

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Resolved [RESOLVED] Storing very large strings

    I recently had to use my "String converter" to convert 20 lines of text to a single line of "VB .Net String coding". For example:

    Code:
    This is line one
    This is line two
    This is line three "with extra stuff"
    
    Empty line above me
    To:
    Code:
    "This is line one" & vbCrLf & "This is line two" & vbCrLf & "This is line three " & chr(34) & "with extra stuff" & chr(34) & vbCrLf & vbCrLf & "Empty line above me"
    What is the best way to represent these types of Strings? For example, if you have to display a long message or just a label with information that changes. I was thinking of some sort of text file collection, but it is a little useless to have 100 text files of information of 5-6 lines...

    How do you guys solve your "long Strings" issue?

  2. #2
    Frenzied Member
    Join Date
    Sep 2006
    Location
    Scotland
    Posts
    1,054

    Re: Storing very large strings

    Why not just use an array with a seperate element for each line?

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

    Re: Storing very large strings

    If you're talking about read-only data then use resources. If you're talking about read/write data then use settings. There's a page for each in the project properties and you access both using the My namespace.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Storing very large strings

    Quote Originally Posted by 03myersd View Post
    Why not just use an array with a seperate element for each line?
    Agreed, something like this maybe.

    Code:
            Dim somelines() As String = New String() {"This is line one", _
                                                      "This is line two", _
                                                      "This is line three ""with extra stuff""", _
                                                      "", _
                                                      "Empty line above me"}
            For Each l As String In somelines
                Debug.WriteLine(l)
            Next
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Storing very large strings

    The my resources way is indeed a nice alternative to text files, never used the My Resources for String variables before.

    Converting it to an array is possible, but then you will have to do it something like this:
    Code:
            Dim newvarlines(4) As String
            newvarlines(0) = "This is line one"
            newvarlines(1) = "This is line two"
            newvarlines(2) = "This is line three " & Chr(34) & "With extra stuff" & Chr(34)
            newvarlines(3) = ""
            newvarlines(4) = "The line above me is empty"
            Dim newvar As String = ""
            For Each line As String In newvarlines
                If newvar <> "" Then newvar &= vbCrLf
                newvar &= line
            Next
    Little long for my taste...

    Maybe go with the '_' method?:
    Code:
            Dim newvar As String = "This is line one" _
            & vbCrLf & "This is line two" _
            & vbCrLf & "This is line three " & Chr(34) & "With extra stuff" & Chr(34) _
            & vbCrLf & "" _
            & vbCrLf & "The line above me is empty"
    Last edited by bergerkiller; Mar 13th, 2011 at 08:30 AM.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Storing very large strings

    Quote Originally Posted by bergerkiller View Post
    The my resources way is indeed a nice alternative to text files, never used the My Resources for String variables before.

    Converting it to an array is possible, but then you will have to do it something like this:
    Code:
            Dim newvarlines(4) As String
            newvarlines(0) = "This is line one"
            newvarlines(1) = "This is line two"
            newvarlines(2) = "This is line three " & Chr(34) & "With extra stuff" & Chr(34)
            newvarlines(3) = ""
            newvarlines(4) = "The line above me is empty"
            Dim newvar As String = ""
            For Each line As String In newvarlines
                If newvar <> "" Then newvar &= vbCrLf
                newvar &= line
            Next
    Little long for my taste...

    Maybe go with the '_' method?:
    Code:
            Dim newvar As String = "This is line one" _
            & vbCrLf & "This is line two" _
            & vbCrLf & "This is line three " & Chr(34) & "With extra stuff" & Chr(34) _
            & vbCrLf & "" _
            & vbCrLf & "The line above me is empty"
    I prefer the way I declared the array. Declaring it the way you have means two changes need to be made. Change the array dimension, and add the string.

    It is still very unclear what you are doing???? What are these long strings? "...but it is a little useless to have 100 text files of information of 5-6 lines..."
    Last edited by dbasnett; Mar 13th, 2011 at 08:39 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Storing very large strings

    It was an example, but to show one, here is what I had to do for the "Help" section in one of my programs. It had a label and two combo boxes with subjects:

    Code:
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            ComboBox2.Items.Clear()
            If ComboBox1.SelectedItem = "Mod launcher" Then
                ComboBox2.Items.Add("Battlefield 2/2142 Installation path")
                ComboBox2.Items.Add("Starting Battlefield 2/2142")
                ComboBox2.Items.Add("Backup system")
                ComboBox2.Items.Add("Error support")
            End If
            If ComboBox1.SelectedItem = "Launch Settings" Then
                ComboBox2.Items.Add("Run Mode")
                ComboBox2.Items.Add("Resolution")
                ComboBox2.Items.Add("Auto Login")
                ComboBox2.Items.Add("Auto Join")
                ComboBox2.Items.Add("Auto Level start")
            End If
            If ComboBox1.SelectedItem = "Single Player Settings" Then
                ComboBox2.Items.Add("Amount bots")
                ComboBox2.Items.Add("Bot skill")
                ComboBox2.Items.Add("Team ratio")
                ComboBox2.Items.Add("Ticket ratio")
                ComboBox2.Items.Add("Score limit")
                ComboBox2.Items.Add("Time limit")
                ComboBox2.Items.Add("Spawn time")
                ComboBox2.Items.Add("Time revivable")
                ComboBox2.Items.Add("Enable free camera")
                ComboBox2.Items.Add("Enable hitindicator")
                ComboBox2.Items.Add("Friendly fire percentage")
                ComboBox2.Items.Add("Auto record a demo")
            End If
            If ComboBox1.SelectedItem = "General Settings" Then
                ComboBox2.Items.Add("Display nametags")
                ComboBox2.Items.Add("Display fps")
                ComboBox2.Items.Add("Backup function")
            End If
        End Sub
    
        Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
            If ComboBox2.SelectedItem = "Battlefield 2/2142 Installation path" Then helplabel.Text = "In order to use LPP Modlauncher, the Battlefield 2/2142 Installation path must be set. You can use the path of the Demo version."
            If ComboBox2.SelectedItem = "Starting Battlefield 2/2142" Then helplabel.Text = "To start a mod, you first select one mod from the 'mods' list. After clicking a mod, the mod will be selected and a description of the mod will be given. You can click the 'launch mod' button to start your mod. It will then launch the game and display a message if the launch was successfull."
            If ComboBox2.SelectedItem = "Error support" Then helplabel.Text = "If you need error support, you can visit our website. The website is displayed in the 'about' tab. You can post your findings and requests there."
            If ComboBox2.SelectedItem = "Run Mode" Then helplabel.Text = "Battlefield 2/2142 can be started in two modes: Fullscreen and Windowed. By using the windowed mode, Battlefield 2/2142 will be launched in a window and debug messages will be shown. Fullscreen mode won't show the debug messages. To skip the into movies, you can check 'Skip movies'."
            If ComboBox2.SelectedItem = "Resolution" Then helplabel.Text = "You can play Battlefield 2/2142 in a resolution. You can use this option if you have a widescreen monitor, to display Battlefield 2/2142 correctly."
            If ComboBox2.SelectedItem = "Auto Login" Then helplabel.Text = "To skip the login screen, you can use auto login. Please notice that your user password is shown in the shortcut and can be retrieved. Do not use this feature on public computers. If you did use this on a public computer, you can erase it with the 'reset settings' button in the settings tab."
            If ComboBox2.SelectedItem = "Auto Join" Then helplabel.Text = "To join a server on startup, you can use the 'auto join' feature. A port is required, if none is shown, you can use the default one: 16567. If the server needs a password, you type a password. If there is no password, leave the line blank. Same as auto login, the password of the server can be retrieved."
            If ComboBox2.SelectedItem = "Auto Level start" Then helplabel.Text = "You can launch a level at startup. To do this, first select a Level and then a gamemode. If you have selected a Conquest gamemode, you can set the maximum of players. In Singleplayer that option is not enabled. If the Level you have selected shows no gamemodes, you can check 'allow custom gamemodes'."
            If ComboBox2.SelectedItem = "Amount bots" Then helplabel.Text = "You can set the amount of bots by dragging the first bar. Notice that more bots require more CPU."
            If ComboBox2.SelectedItem = "Bot skill" Then helplabel.Text = "You can set the strenght of the bots by dragging the second bar."
            If ComboBox2.SelectedItem = "Team ratio" Then helplabel.Text = "You can set the team ratio by dragging the third bar. By positioning the bar in the middle, each team has the same amount of players. If your looking for a challenge, drag the bar to the left. If you want an easy game, drag the bar to the right."
            If ComboBox2.SelectedItem = "Ticket ratio" Then helplabel.Text = "You can set the ticket ratio by dragging the fourth bar. By positioning the bar in the middle, the default amount of tickets will be used. Position it to the right to use more tickets, position it to the left to use less tickets."
            If ComboBox2.SelectedItem = "Score limit" Then helplabel.Text = "By using a score limit, the game will end when one player reaches a certain score. Do not set this too low, or the game will end too quickly."
            If ComboBox2.SelectedItem = "Time limit" Then helplabel.Text = "By using a time limit, the timer will appear and when it reaches 0, the game will end. It is not recommended to set this timer too low."
            If ComboBox2.SelectedItem = "Spawn time" Then helplabel.Text = "The spawn time is the time it takes to spawn after your killed. By unchecking or setting the time to 0 you can respawn emediately after you died."
            If ComboBox2.SelectedItem = "Time revivable" Then helplabel.Text = "You set the time medics can revive you by settings 'time revivable'. If you want an instant-kill game, you can uncheck this settings or set the time to 0."
            If ComboBox2.SelectedItem = "Enable free camera" Then helplabel.Text = "If you want to look around while your killed, you can check 'Enable free camera'."
            If ComboBox2.SelectedItem = "Enable hitindicator" Then helplabel.Text = "If you want to make the game real, you can uncheck the hit indicator by unchecking 'Enable hitindicator'."
            If ComboBox2.SelectedItem = "Friendly fire percentage" Then helplabel.Text = "If you hate it being killed by your team, you can set the damage percentage of friendly damage. You can set the damage done by vehicles and damage done by soldier fire."
            If ComboBox2.SelectedItem = "Auto record a demo" Then helplabel.Text = "You can record your Single Player games automaticly. The quality can be set. Use a high quality for a smooth recording. Use a low quality for performance during the game."
            If ComboBox2.SelectedItem = "Display nametags" Then helplabel.Text = "If you want to show the player names, you can check 'Display Nametags'."
            If ComboBox2.SelectedItem = "Display fps" Then helplabel.Text = "If you want to display the current framerate of Battlefield 2/2142, you can check 'Display FPS'."
            If ComboBox2.SelectedItem = "Backup system" Then helplabel.Text = "The first time the LPP Modlauncher starts, a backup will be made of all the files of all the mods the modlauncher modifies. This is important incase you can no longer join internet servers. To restore, go to the 'settings' tab and click the 'restore settings' button. Then you select a backup and you select which mods to restore. To make a new backup, click the 'make backup' button. When you save in the 'general settings' window, a backup will be made as well. Please note that backups might not work properly when you switch between the retail version and the Demo."
            If ComboBox2.SelectedItem = "Backup function" Then helplabel.Text = "Every time you save in the 'general settings' window, a backup will be made. This is done because the settings will be saved in the file 'GameLogicInit.con', and when this file is modified, Punkbuster may kick you from the server and you would have to re&#239;nstall the game. To ensure you won't have to re&#239;nstall, LPP Modlauncher makes a backup. See 'Mod launcher-Backup system' for more information."
        End Sub
    A liiiiitle sick

    In a later app I had to add "code information" for the code text box. It now has around 200 text files containing syntax like this:
    Code:
    { }
    GeometryTemplate.create <StaticMesh> Test
    Load a new geometry
    
    The type of mesh
    { }
    GeometryTemplate.create StaticMesh <Test>
    Load a new geometry
    
    The file name of the mesh
    {}
    Sow, I could store all info in arrays but that would make the code very long...
    Storing in text files result in loading it into the program's resources...that takes long. In the app that used 200 text files I made it only load the file it had to load during run-time.
    I was thinking of some sort of "String library", just as I made a "Image library" once. Just don't know how to make it as fast as possible and to allow quick loading of the data...

    Sorta like a built-in function:
    - Search library header for requested variable name
    - Read string expression from the bounds defined in the header
    - Close and return the string expression

    Any ways, I need to find out a way to QUICKLY load the String in a LARGE collection of LARGE Strings.
    Preferably modifiable as well...
    Last edited by bergerkiller; Mar 13th, 2011 at 09:01 AM.

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Storing very large strings

    What does the code have to do with the text file format? A very large string would be 1GB.

    There are other things you could do to make your code faster. This pattern

    Code:
    )
            If ComboBox1.SelectedItem = "Mod launcher" Then
                'lines removed
            End If
            If ComboBox1.SelectedItem = "Launch Settings" Then
                'lines removed
            End If
            If ComboBox1.SelectedItem = "Single Player Settings" Then
                'etc.
    seems to imply that ComboBox1.SelectedItem can be multiple things at the same time. This would be better I think

    Code:
    )
            If ComboBox1.SelectedItem = "Mod launcher" Then
                'lines removed
            ElseIf ComboBox1.SelectedItem = "Launch Settings" Then
                'lines removed
            ElseIf ComboBox1.SelectedItem = "Single Player Settings" Then
                'etc.
    You have a similar pattern for CB2.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Storing very large strings

    I know, it was made when I first started coding .NET
    It was working, so I didn't mind changing it.

    I actually need some sort of way to represent the strings used. Since storing it in a file brings the read speed factor, it has to be stored in memory.

    I managed to get some sort of resolution:
    Code:
    Public Class Information
        Public Shared ReadOnly Property HelpInfoErrorSupport()
            Get
                Return "If you need error support, you can visit our website." _
                & vbCrLf & "The website is displayed in the 'about' tab." _
                & vbCrLf & "You can post your findings and requests there."
            End Get
        End Property
        Public Shared ReadOnly Property HelpInfoInstallationPath()
            Get
                Return "In order to use LPP Modlauncher, the Battlefield 2/2142 Installation path must be set." _
                & vbCrLf & "You can use the path of the Demo version."
            End Get
        End Property
    End Class
    Code:
    MessageBox.Show(Information.HelpInfoErrorSupport, "Error Support", MessageBoxButtons.OK, MessageBoxIcon.Information)
    But I am actually more interested in what you guys would do. Say, you have to display changing information in your program or you want to use "localization" (different languages).

    I just can't believe anyone would use arrays to represent their information...

  10. #10
    Addicted Member
    Join Date
    Oct 2009
    Posts
    212

    Re: Storing very large strings

    There are plenty of examples of localization around the net. I recall early on when I was learning vb.net someone basically said never have a string in your code, always use string resources, so that your code can be localized.
    When I was cutting my teeth on DNN I was exposed to resx files all the time, every string was in a resx, it seems like a good design principle, but also seems like a good bit of work for simple projects.

    I was also told dont use mulitple if/then where you could use a case select, as case select is faster for complex if/thens. But honestly I cant say I have bench marked the difference.
    Have you tried Google?

  11. #11
    Addicted Member
    Join Date
    Oct 2009
    Posts
    212

    Re: Storing very large strings

    After I went to bed last night I realized another way I have "created" larger strings. Use the VS designer, and set the text via the properties in the designer(including using copy/paste from say notepad). Then open up the class.designer.vb and find the text, you'll find the VS IDE has properly formatted the text, you can then copy and paste the formatted text to your string variable.
    Code:
            Me.logTextBox.Text = "Hello," & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "This is a test, and only a test" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "Thanks!"
    Have you tried Google?

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Storing very large strings

    Hehe too much of a hassle, I just made a small app for it that replace " chars by Chr(34) and lines to VbCrlf, formatted with & between parts. Works great.

    Guess I'll be working more with storing my large strings in properties inside classes, so I can quickly change the way things are represented. For example, inside the property I can always change it to load the string from a localization file in the end.

  13. #13
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Storing very large strings

    Quote Originally Posted by bergerkiller View Post
    I know, it was made when I first started coding .NET
    It was working, so I didn't mind changing it.

    I actually need some sort of way to represent the strings used. Since storing it in a file brings the read speed factor, it has to be stored in memory.

    I managed to get some sort of resolution:
    Code:
    Public Class Information
        Public Shared ReadOnly Property HelpInfoErrorSupport()
            Get
                Return "If you need error support, you can visit our website." _
                & vbCrLf & "The website is displayed in the 'about' tab." _
                & vbCrLf & "You can post your findings and requests there."
            End Get
        End Property
        Public Shared ReadOnly Property HelpInfoInstallationPath()
            Get
                Return "In order to use LPP Modlauncher, the Battlefield 2/2142 Installation path must be set." _
                & vbCrLf & "You can use the path of the Demo version."
            End Get
        End Property
    End Class
    Code:
    MessageBox.Show(Information.HelpInfoErrorSupport, "Error Support", MessageBoxButtons.OK, MessageBoxIcon.Information)
    But I am actually more interested in what you guys would do. Say, you have to display changing information in your program or you want to use "localization" (different languages).

    I just can't believe anyone would use arrays to represent their information...
    This code won't even compile, so I assume it was just an example.

    This worked

    Code:
    Public Class Information
    
        Private Shared ReadOnly _HelpInfoErrorSupport As String = String.Join(Environment.NewLine, New String() _
                                                                              {"If you need error support, you can visit our website.", _
                                                                               "The website is displayed in the 'about' tab.", _
                                                                               "You can post your findings and requests there."})
    
        Private Shared ReadOnly _HelpInfoInstallationPath As String = String.Join(Environment.NewLine, New String() _
                                                                                  {"In order to use LPP Modlauncher, the Battlefield 2/2142 Installation path must be set.", _
                                                                                   "You can use the path of the Demo version."})
    
        Public ReadOnly Property HelpInfoErrorSupport() As String
            Get
                Return _HelpInfoErrorSupport
            End Get
        End Property
    
        Public ReadOnly Property HelpInfoInstallationPath() As String
            Get
                Return _HelpInfoInstallationPath
            End Get
        End Property
    
    End Class
    If this were me I would have an XML file


    Code:
        <Messages> 
    
         <Message>
         <Name>Name_of_the_message</Name>  
         <text>Message text</text>
        </Message>
    
         <Message>
         <Name>Name_of_the_message</Name>  
         <text>Message text</text>
        </Message>
    
        </Messages>
    Unless the number of messages is incredibly large the file access time would be negligible.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Storing very large strings

    Cool trick with the String.Join. I always thought all that function did was the same as the "&" operator.

    Thanks for the replies everyone, now got quite a lot of new ways to store large Strings in both code, resources and file. I really like that String.Join(vbNewLine, Array_of_lines) code, never used it before and it would take the "vbCrlf" usage down to a single time.

    Sometimes I forget to look "outside the box", since I am used to add string elements together using & signs.

Tags for this Thread

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